【发布时间】:2020-02-20 12:37:26
【问题描述】:
我有一年以来一直在维护的 Discord 机器人,几个月前我更改了一些文件结构以清理它,让我更容易知道发生了什么。
问题是,每当我尝试请求位于机器人根目录中的文件夹中的文件(带有 require)时,有时它与 "./" 一起使用,而其他时候它与 "../" 一起使用
当前文件结构为:
----commands
-------commands.js(multiple files)
----images
-------halloween
----------images.png/jpg(multiple images)
----logs
-------bot.log
----modules
------logger.js
----settings
-------config.json
-emojis.json
-gifs.json
-index.js
按照上面的结构,例如,当我尝试在命令中请求其中一个万圣节图像时,对我来说合乎逻辑的事情是使用“../images/halloween/image.png”,但我有使用“./images/halloween/image.png”,就好像“images”文件夹在“commands”文件夹中一样
在我必须使用的命令之一中:
const logs = require("../modules/logger");
const background = await Canvas.loadImage("./images/halloween/background.jpg");
我想知道为什么会这样。看到一个错误说找不到文件只是因为node.js决定这次父目录是"./"而不是"../"
【问题讨论】:
-
请求什么?
require,readFile,或其他什么。? -
有要求,但有时我两个都不用,这取决于我使用的包
-
我们能看到有时失败的代码吗?..
-
我用示例代码编辑了帖子
-
这不是使用
require来加载,它使用的是Canvas.loadImage,我不确定那是什么,但是对于readFile之类的东西,它可能会使用try and Avoid using relative这里的路径,相对于当前工作目录的路径,并且可以在程序执行期间更改。而是使用path.join和__dirname.. 例如。loadImage(path.join(__dirname, './images/halloween/background.jpg'))
标签: javascript node.js directory discord.js directory-structure