0

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' ]
}

gest

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.

2
  • Can you edit your question to include your directory structure? (external-content.duckduckgo.com/iu/…) Commented Apr 20, 2020 at 3:39
  • Not sure how to make it look exactly like that in StackOverFlow, but I sent a screen shot of how it looks in Visual Studio Code. Hope that works for what you were asking Commented Apr 20, 2020 at 5:52

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Just wanted to make sure, since it's seeming to be giving me an error. Would moving the commands folder into the discordBot folder and leaving: ./const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js')); like that what you're saying, because it's telling me there's no file / directory when I move it in. How would I have to write that line?
If you move the folder, you can leave the relative path with ./. However is it international, that you write ./const? That one seems a bit weird, the rest looks right tho. Perhaps you need to restart your bot and/or IDE.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.