#Error: Cannot find module

7 messages · Page 1 of 1 (latest)

lucid rover
#

My reload command cannot find the command file that I'm looking for no matter what I try. Yes, this is d.js but this is an entirely js related question

#
const fs = require('node:fs');

module.exports = {
    name: "reload",
    aliases: ["rl"],
    async execute(client, message, args) {

        const cmd_args = args.shift().toLowerCase();
        const cmd = client.prefix_commands.get(cmd_args);

        if (!cmd) return message.channel.send("That command does not exist.");
        fs.readdirSync('./commands/prefix').forEach(dir => {
            // Reads each Directory in my Commands/Prefix folder and gets every .js file in each subdirectory
            const commands = fs.readdirSync(`./commands/prefix/${dir}`).filter(file => file.endsWith('.js'))

            for (let command of commands) {

                // Removes the .js. Ex: ping.js -> ping
                let com = command.substring(0, command.length - `.js`.length);
  
                // Checks if the argument entered is the same as one of the files in the Command/Prefix subdirectories
                if (cmd_args === com) {
                    // Error caused by line below
                    delete require.cache[require.resolve(`./commands/prefix/${dir}/${command}`)];

                    try {
                        ...
                    } catch (error) {
                        ...
                    }

                }
            }
        })

    },
};
#

I have tried:
./commands/prefix/${dir}/${command}
../commands/prefix/${dir}/${command}
./prefix/${dir}/${command}
./${dir}/${command}
command

#

I can Ctrl Click in VSC and it will take me to the file that I'm looking for but my code says it cannot find it

#

Alright. Figured it out.

#

I logged my require cache and noticed that each required file had the working folder included in the variable

#

./commands/prefix/${dir}/${command} -> ${process.cwd()}/commands/prefix/${dir}/${command}