I wanted to read all of the files that are in a Folder, but I got an error. My code:
const Commands = []
readdirSync("../commands").forEach((folder) => {
readdirSync(`../commands/${folder}`).forEach((file) => {
const command = require(`../commands/${folder}/${file}`);
if (!command.name) return;
Commands.push(command);
})
})
The error:
<rejected> Error: ENOENT: no such file or directory, scandir '../commands'
I am using discord.js v13 and node.js v16 Notes: main.js is not in the commands folder.
In node, the relative paths for fs functions are from the path of the entrypoint file (your index.js), so in this case, instead of putting ../commands (which would be correct if you started your command with commands.js, but you have not), you need to put ./commands (I am assuming the commands folder is in the same folder as your index.js)
I found the solution myself. I just needed to replace that code with this one:
const Commands = []
readdirSync("./commands").forEach((file) => {
const command = require(`../commands/${file}`);
if (!command.name) return;
Commands.push(command);
})