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

23 messages · Page 1 of 1 (latest)

rose coral
const commands = []
const commandFolders = fs.readdirSync('./commands');

for (const folder of commandFolders) {
    const folderPath = `./commands/${folder}`;
    const commandFiles = fs.readdirSync(folderPath).filter(file => file.endsWith('.js'));
    for (const file of commandFiles) {
        const command = require(`${folderPath}/${file}`);
        commands.push(command.data.toJSON());
    }
}```

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

rose coral

error:

balmy bison

fs reads relative paths from working dir

rose coral

i dont really know why fs cant find that path

balmy bison

working dir is where you start the process from, which is (probably) the one package.json is in

./ will refer to that directory, which has no "commands" folder

rose coral
balmy bison

again, it starts at the current working directory.

so now you are moving one folder out of that (../), which probably doesn't even have a src

rose coral

ah

ah ok

now i understand

rose coral

having this now here is the updated code:```js
const commands = []
const commandFolders = fs.readdirSync('./src/commands');

for (const folder of commandFolders) {
const folderPath = ./src/commands/${folder};
const commandFiles = fs.readdirSync(folderPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(${folderPath}/${file});
commands.push(command.data.toJSON());
}
}```

balmy bison

require reads from the module it is used in, not cwd

slender bloom

try that

Client.commands = new Discord.Collection()
const commandFolders = fs.readdirSync(`./commands/`).sort((a, b) => {
    return parseInt(a.match(/\d+/)) - parseInt(b.match(/\d+/))
})

for (const folder of commandFolders) {
    const commandFiles = fs.readdirSync(`./commands/${folder}`).filter(file => file.endsWith('.js')).sort((c, d) => {
        return parseInt(c.match(/\d+/)) - parseInt(d.match(/\d+/))
    })

    for (const file of commandFiles) {
        const command = require(`./commands/${folder}/${file}`)
        Client.commands.set(command.name, command)
        console.log(`Command load: ${command.name} [${folder}]`)
    }
}


slender bloom

😉

rose coral

did this

solved the issue

1blade you dont need all that stuff just needed to fix the paths