我如何使require()指向文件的直接路径 [英] How do I make require() take a direct path to a file
问题描述
所以我有以下代码,问题是当我遍历数组中的每个文件并尝试要求文件路径时,它给我一个找不到模块的错误.
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()
递归函数有效,并且文件只是路径的字符串.但是经过研究,require()需要相对路径,而不是直接路径.所以我认为我需要对fs进行一些操作,以使文件路径成为相对路径?我在任何地方都找不到答案.
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.
谢谢!
推荐答案
require
根本没有路径.标准加载程序只是根据您的算法,按照一系列模式使用您给它提供的字符串.
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.
您想要的是在磁盘上加载并执行给定的Lua脚本.那不是拼写的require
;拼写为dofile
.
What you want is to load and execute a given Lua script on disk. That's not spelled require
; that's spelled dofile
.
这篇关于我如何使require()指向文件的直接路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!