How do I make require() take a direct path to a file

做~自己de王妃 提交于 2019-12-10 23:32:52

问题


so I have the following code, and the problem is that when I loop through each file in my array and try to require the file path, it gives me an error of the module isn't found.

local Commands = {}

function getCommands()
    local readdir = fs.readdir
    local readdirRecursive = require('luvit-walk').readdirRecursive
    readdirRecursive('./Desktop/Discord/ArtifexBot/Discordia/resources/commands/', function(k, files) 
        for i,v in pairs(files) do
            if v:match(".lua") and not v:match("commands.lua") then
                local cmd = v:match("([^/]-)%..-$")
                fs.readlink(v,function(err,thing)
                    print(err,thing)
                end)
                Commands[cmd] = require(v)
            end
        end
    end)
end
getCommands()

The recursive function works, and the files are just strings of the path. But after research, require() needs a relative path, not a direct path. So I think I need to do something with fs to make the file path a relative path instead? I couldn't find the answer anywhere.

Thanks!


回答1:


require doesn't take a path at all. The standard loaders simply use the string you give it in a sequence of patterns, in accord with its algorithm.

What you want is to load and execute a given Lua script on disk. That's not spelled require; that's spelled dofile.



来源:https://stackoverflow.com/questions/38640069/how-do-i-make-require-take-a-direct-path-to-a-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!