简体   繁体   English

错误:ENOENT:没有这样的文件或目录,scandir '../commands' discord.js

[英]Error: ENOENT: no such file or directory, scandir '../commands' discord.js

I want to read all of the files that are in a directory, but I got an error.我想读取目录中的所有文件,但出现错误。 My code:我的代码:

        const Commands = []
        readdirSync("../commands").forEach((folder) => {
            readdirSync(`../commands/${folder}`).forEach((file) => {
                const command = require(`../commands/${folder}/${file}`);
                if (!command.name) return;
                Commands.push(command);
            })
        })

The error:错误:

<rejected> Error: ENOENT: no such file or directory, scandir '../commands'

I am using discord.js v13 and node.js v16我正在使用 discord.js v13 和 node.js v16

Notes : main.js is not in the commands folder.注意:main.js 不在命令文件夹中。

在此处输入图像描述

In node, the relative paths for fs functions are from the path of the entrypoint file (your index.js), so in this case, instead of putting ../commands (which would be correct if you started your command with commands.js, but you have not), you need to put ./commands (I am assuming the commands folder is in the same folder as your index.js)在节点中,fs 函数的相对路径来自入口点文件(您的 index.js)的路径,因此在这种情况下,而不是放置../commands (如果您使用 commands.js 启动命令,这将是正确的,但你没有),你需要放置./commands (我假设命令文件夹与你的 index.js 在同一个文件夹中)

I found the solution myself.我自己找到了解决方案。 I just needed to replace that code with this one:我只需要用这个替换那个代码:

const Commands = []
        readdirSync("./commands").forEach((file) => {
                const command = require(`../commands/${file}`);
                if (!command.name) return;
                Commands.push(command);

        })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM