Node.js 无法在另一个文件夹中找到文件
Node.js is unable to find a file in another folder
我有一个我维护了一年的 Discord 机器人,几个月前我稍微更改了文件结构以清理它并让我更容易知道发生了什么。
问题是,每当我尝试请求位于 bot 根目录的文件夹中的文件(需要)时,有时它与 "./"
一起工作,而其他时候它与 [=14 一起工作=]
当前文件结构为:
----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 决定这次父目录是 "./"
而不是 "../"
假设您的命令文件正在进行文件系统调用(因为您正在从中访问图像),您从中调用脚本的目录可能很重要。确保您正在使用 path
实用程序来解析您的文件位置。有关详细信息,请参阅 。
我有一个我维护了一年的 Discord 机器人,几个月前我稍微更改了文件结构以清理它并让我更容易知道发生了什么。
问题是,每当我尝试请求位于 bot 根目录的文件夹中的文件(需要)时,有时它与 "./"
一起工作,而其他时候它与 [=14 一起工作=]
当前文件结构为:
----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 决定这次父目录是 "./"
而不是 "../"
假设您的命令文件正在进行文件系统调用(因为您正在从中访问图像),您从中调用脚本的目录可能很重要。确保您正在使用 path
实用程序来解析您的文件位置。有关详细信息,请参阅