Reputation: 21
So. I've been searching for a while, and couldn't seem to find anything that particularly applies. So far, I've got this:
const fs = require('fs');
const Discord = require('discord.js');
const { prefix, token } = require('./config.json');
const client = new Discord.Client();
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);
}
Then, in my second file, located in commands, which is named ping.js
module.exports = {
name: 'ping',
description: 'Ping!',
cooldown: 5,
execute(message) {
message.channel.send('Pong.');
},
};
The error that I've been getting is the following,
internal/modules/cjs/loader.js:979
throw err;
^
Error: Cannot find module './commands/hello.js'
Require stack:
- /Users/SurajAnand/Desktop/Discord/discordBot/index.js
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:976:15)
at Function.Module._load (internal/modules/cjs/loader.js:859:27)
at Module.require (internal/modules/cjs/loader.js:1036:19)
at require (internal/modules/cjs/helpers.js:72:18)
at Object.<anonymous> (/Users/SurajAnand/Desktop/Discord/discordBot/index.js:11:18)
at Module._compile (internal/modules/cjs/loader.js:1147:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1167:10)
at Module.load (internal/modules/cjs/loader.js:996:32)
at Function.Module._load (internal/modules/cjs/loader.js:896:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ '/Users/SurajAnand/Desktop/Discord/discordBot/index.js' ]
}
I've just started to get into JS for the sake of making a fun little bot to mess with some friends. I tried to run this piece of code off of the discord.js guide, and it seems incredibly cool... Just that I can't seem to get it to work, so any help would be very appreciated.
Upvotes: 0
Views: 1990
Reputation: 1690
For me it looks like just the relative path is wrong.
If you call ./commands/hello.js
in index.js
, it will look in discordBot/commands/hello.js
, which obviously doesn't exist. Change it from ./commands/hello.js
to ../commands/hello.js
, this should work.
Another option would be moving the commands
folder into discordBot
,which may be a bit cleaner but that is up to you to decide
Upvotes: 2