Home > Enterprise >  How do I Fix a Cannot find module '.functions/handleCommands.js' error?
How do I Fix a Cannot find module '.functions/handleCommands.js' error?

Time:12-11

Upon running my Bot to have it sign-on after installing a command and event handler I got the error

Error: Cannot find module '.functions/handleCommands.js'

Require stack:

C:\Users\levan\Desktop\Discordbot\Bot Templates\2 command handling\src\bot.js

at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:999:19)
at require (node:internal/modules/cjs/helpers:102:18)
at C:\Users\levan\Desktop\Discordbot\Bot Templates\2 command handling\src\bot.js:14:9
at Object.<anonymous> (C:\Users\levan\Desktop\Discordbot\Bot Templates\2 command handling\src\bot.js:19:3)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1147:10)
at Module.load (node:internal/modules/cjs/loader:975:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12) {   code: 
'MODULE_NOT_FOUND',   
requireStack: [
'C:\\Users\\levan\\Desktop\\Discordbot\\Bot Templates\\2 command handling\\src\\bot.js'

After looking thru the folder functions and the handleCommands.js, I Still cannot figure out what I'm missing or what I might on messed up. I thought that maybe my module.exports was in the wrong place but moving things around did not solve the issue either.

const { Routes } = require('discord-api-types/v9');
const fs = require('fs');
const clientId = 'redacted'; // your bots token
const guildId = 'redacted'; // the server id the bot will be on

module.exports = (clinet) => {
    clinet.handleCommands = async (commandFolders, path) =>{
        clinet.commandArray = [];
        for(folder of commandFolders){
            const commandFiles = fs.readdirSync(`${path}/${folder}`).filter(file => file.endsWith('.js'));
                for (const file of commandFiles) {
                    const command = require(`../commands/${folder}/${file}`);
                    // Set a new item in the Collection
                    // With the key as the command name and the value as the exported module
                    client.commands.set(command.data.name, command);
                    clinet.commandArray.push(command.data.toJSON());
            }
        }
        
    };
};

const rest = new REST({ version: '9' }).setToken(process.env.token);

(async () => {
    try {
        console.log('Started refreshing application (/) commands.');

        await rest.put(
            Routes.applicationGuildCommands(clientId, guildId),
            { body: clinet.commandArray },
        );

        console.log('Successfully reloaded application (/) commands.');
    } catch (error) {
        console.error(error);
    }
})();

As the error was MODULE_NOT_FOUND I figured that maybe I didn't run all the installs I need to make my bot work. After running thru my npm lists the error persisted.

NPM's Installed

  • "npm init"
  • "npm install discord.js"
  • "npm install dotenv"
  • "npm install discord.js dotenv"
  • "npm install @discordjs/rest discord-api-types @discordjs/builders"

My goal is to be the Bot back online and to figure out how to troubleshoot this sort of error in case it comes up again. Any help you can give will be greatly appreciated.

CodePudding user response:

'.functions/handleCommands.js' should have been './functions/handleCommands.js' Caffeine is not a substitute for sleep

  • Related