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

2019-07-13 01:57发布

问题:

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.



标签: lua luvit