Error: Invalid string format | const command = require(`${file}`);

Viewed 381

I have tried many ways but still unsuccessful...

this is the error:

Error: Invalid string format
    at Object.run (/home/runner/discordgame-1/node_modules/@sapphire/shapeshift/dist/index.js:1281:64)
    at /home/runner/discordgame-1/node_modules/@sapphire/shapeshift/dist/index.js:113:66
    at Array.reduce (<anonymous>)
    at StringValidator.parse (/home/runner/discordgame-1/node_modules/@sapphire/shapeshift/dist/index.js:113:29)
    at validateName (/home/runner/discordgame-1/node_modules/@discordjs/builders/dist/index.js:885:17)
    at MixedClass.setName (/home/runner/discordgame-1/node_modules/@discordjs/builders/dist/index.js:957:5)
    at Object.<anonymous> (/home/runner/discordgame-1/commands/ping.js:5:6)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)

code:

for (const file of commandFiles) {
    const command = require(`${file}`);
    commands.push(command.data.toJSON());
  client.commands.set(command.data.name, command);
}

There must be something wrong with const command = require(${file});

I'm using stackoverflow for the first time because I really can't find a solution.

If you want more information on this issue, ask me.

1 Answers

I ran into the same problem while studying about discordjs. I was trying to create some discord commands, and the problem was the name I chose to a command in setName(), I was using a capital letter:

module.exports = {
    data: new SlashCommandBuilder()
        .setName('membersQuantity') // the uppercase Q is the problem
        .setDescription('members quantity'),
    async execute(interaction) {
        await interaction.reply('test');
    },
};

so I just changed it:

module.exports = {
    data: new SlashCommandBuilder()
        .setName('membersquantity') //no uppercase characters anymore
        .setDescription('members quantity'),
    async execute(interaction) {
        await interaction.reply('test');
    },
};

and it fixed my problem.

There are probably more not allowed characters in the name of Slash Commands, so I would suggest to always use lowercase characters only.

Related