【问题标题】:Error: ENOENT: no such file or directory, scandir './commands/'错误:ENOENT:没有这样的文件或目录,scandir './commands/'
【发布时间】:2021-04-27 16:54:17
【问题描述】:

当我想使用不和谐时,我得到了这个错误输出:

Error: ENOENT: no such file or directory, scandir './commands/'
←[90m    at Object.readdirSync (fs.js:1021:3)←[39m
    at Object.<anonymous> (C:\Users\DAA23\OneDrive\Desktop\bot\main.js:11:25)
←[90m    at Module._compile (internal/modules/cjs/loader.js:1063:30)←[39m
←[90m    at Object.Module._extensions..js 
(internal/modules/cjs/loader.js:1092:10)←[39m
←[90m    at Module.load (internal/modules/cjs/loader.js:928:32)←[39m
←[90m    at Function.Module._load 
(internal/modules/cjs/loader.js:769:14)←[39m
←[90m    at Function.executeUserEntryPoint [as runMain] 
(internal/modules/run_main.js:72:12)←[39m
←[90m    at internal/main/run_main_module.js:17:47←[39m {
  errno: ←[33m-4058←[39m,
  syscall: ←[32m'scandir'←[39m,
  code: ←[32m'ENOENT'←[39m,
  path: ←[32m'./commands/'←[39m
}
    const Discord = require('discord.js')
    const client = new Discord.Client();
    const prefix = '*';
    const fs = require('fs');

    client.commands = new Discord.Collection();

    const commandFiles = fs.readdirSync('./commands/').filter(file => 
    file.endsWith('.js'));

    for(const file of commandFiles){
        const command = require(`./commands/${file}`);
        client.commands.set(command.name, command);
    }


    client.once('ready', () => {
        console.log('ur bot is on');
    });

    client.on('message', message =>{
    if(!message.content.startsWith(prefix) ||message.author.bot) return;

    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();

    if(command === 'loop'){
        message.channel.send('-loop');
    };

});

client.login('xxxxx.xxxxxx');

所以我正在尝试创建一个不和谐机器人,之前它说找不到 discord.js,所以我执行了“npm install discord.js”并删除了不和谐错误,但是它引入了这个错误,我不知道如何修理它。我尝试重新安装 nodejs 并重新安装 discord,但没有任何改变。

【问题讨论】:

  • 您好,欢迎来到 SO!从快速超越此错误看来,您的 main.js 文件中可能存在错误。你能包括你的项目结构吗?还有一些代码?
  • 确定我已经在错误下面添加了我的 main.js 代码
  • 该错误表示您尝试在./commands 上使用readdirSync,但找不到目录。你能包括你的项目结构/文件夹结构吗?
  • 文件夹结构是什么意思?你想要 main.js 的目录还是其他的目录?对不起,如果这真的很基础,但我才刚刚开始,我正在努力学习。
  • 是的,您的项目的文件夹结构如何?根文件夹?你有什么文件和文件夹?

标签: javascript node.js discord.js


【解决方案1】:

根据您给我的信息,我认为您只是缺少commands 目录。在您的代码中,您尝试读取 commands 目录中的文件,但由于它不存在,因此会引发错误。

我假设您的项目结构如下所示(根据您的评论)

/
  node_modules/
  main.js
  package.json

如果您尝试从main.js 文件中读取名为commands 的目录中的文件,则需要先实际创建该目录。所以你的结构应该是这样的

/
  node_modules/
  commands/
  main.js
  package.json

然后要读取命令目录中的文件,你可以试试这个

const path = require('path');
const fs = require('fs');

const files = fs.readdirSynch(path.resolve(__dirname, 'commands'));

【讨论】:

  • 是的,您是正确的,我重写了文件,但我缺少一个“命令文件夹”,感谢您的帮助和您的时间。
猜你喜欢
  • 2021-12-04
  • 2022-09-30
  • 2017-12-28
  • 2021-12-05
  • 1970-01-01
  • 2022-06-28
  • 2019-10-14
  • 2019-09-07
  • 1970-01-01
相关资源
最近更新 更多