Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
452 views
in Technique[技术] by (71.8m points)

javascript - Error: ENOENT: no such file or directory, scandir './commands/'

i am getting this error output, when i want to use discord:

Error: ENOENT: no such file or directory, scandir './commands/'
←[90m    at Object.readdirSync (fs.js:1021:3)←[39m
    at Object.<anonymous> (C:UsersDAA23OneDriveDesktopotmain.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');

So I am trying to create a discord bot and previously it said cannot find discord.js so I did 'npm install discord.js' and that removed the discord error however it introduced this error and I don't know how to fix it. I have tried reinstalling nodejs and also reinstalling discord but nothing has changed.

question from:https://stackoverflow.com/questions/65858441/error-enoent-no-such-file-or-directory-scandir-commands

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Based on the information you gave me I think your just missing the commands directory. In your code you are trying to read files in the commands directory, but since it does not exist it throws the error.

I assume your project structure looks like this (bases on your comment)

/
  node_modules/
  main.js
  package.json

If you are trying to read the files inside a directory called commands from within the main.js file, you need to actually create the directory first. So your structure should look like this

/
  node_modules/
  commands/
  main.js
  package.json

Then to read the files inside the commands directory you can try this

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...