FinallyComplete
FinallyComplete

Reputation: 124

Error: ENOTDIR: not a directory, scandir error on interactionCreate

All of my files from Commands are read in fine but I get an error from 'interactionCreate.jslocated inEvents`

node:internal/fs/utils:343
    throw err;
    ^

Error: ENOTDIR: not a directory, scandir './Events/interactionCreate.js'

My Event.js file is as follows:

const { readdirSync } = require('fs');
const ascii = require('ascii-table');
let table = new ascii("Events");
table.setHeading('EVENTS', ' LOAD STATUS');

module.exports = (client) => {
    readdirSync('./Events/').forEach(dir => {
        const events = readdirSync(`./Events/${dir}`).filter(file => file.endsWith('.js'));
        for(let file of events) {
            let pull = require(`../Events/${dir}/${file}`);
            if(pull.name) {
                client.events.set(pull.name, pull);
            } else {
                table.addRow(file, 'EVENT REGISTERED')
                continue;
            } if(pull.aliases && Array.isArray(pull.aliases)) pull.aliases.forEach(alias => client.aliases.set(alias, pull.name))
        }
    });
    console.log(table.toString());
}

Upvotes: 0

Views: 9505

Answers (1)

mpen
mpen

Reputation: 282845

Your problem is here:

    readdirSync('./Events/').forEach(dir => {
        const events = readdirSync(`./Events/${dir}`)

readdirSync will return all the entries in the Events dir, that includes both files and directories. You've named your variable dir but they aren't all dirs. This is evidenced by the error message which specifically states ./Events/interactionCreate.js is not a directory.

Either remove non-dirs from your Events directory (i.e. move that file), or better, check if dir is in fact a directory before calling readdirSync on it.

The easiest way to do that is to add the {withFileTypes: true} option, and then you can call dir.isDirectory()

See docs https://nodejs.org/api/fs.html#fsreaddirsyncpath-options

Upvotes: 1

Related Questions