Skip to content
Advertisement

Why can’t it locate the directory?

Was cooding a command handler in discord.js but ran into some trouble along the way, from my limited knowledge in javascript and discord.js the code should at least be able to find the directory.

enter image description here

Said code that’s causing the headache:

const cmdFiles = fs.readdirSync('../cmds/').filter(file => file.endsWith('.js'));
for(const file of cmdFiles) {
    console.log("hej");
    console.log(file);
    const cmd = require(`../cmds/${file}`);

    client.cmds.set(cmd.name, cmd);
}

Advertisement

Answer

The path passed to fs.readdirSync() is interpreted as relative to the current working directory, not to the JS file, so you probably should replace ../cmds/ by ./cmds/ or simply cmds/.

You can change the working directory by running the cd command before running Node.js.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement