A1X1
A1X1

Reputation: 35

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:\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');

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.

Upvotes: 2

Views: 6975

Answers (1)

turbopasi
turbopasi

Reputation: 3605

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.readdirSync(path.resolve(__dirname, 'commands'));

Upvotes: 2

Related Questions