Len Creemers
Len Creemers

Reputation: 3

discord.js keeps receiving TypeError: Cannot read property 'name' of undefined

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

I keep on getting an error

TypeError: Cannot read property 'name' of undefined

and it has been working flawlessly until now. I tried to console.log(command.data) but I can't seem to find what's wrong...

Upvotes: 0

Views: 1827

Answers (2)

Luuk
Luuk

Reputation: 711

This error is telling that the command.data.name line doesn't exists in the file. So this error does not show up because of your index.js file but because one of the files that you are trying to read commands folder. You'll need to make sure that you have added in every file that is being read in the index.js (so in your case all the files in your commands file) in the module.exports the following json line:

module.exports = {
     data: {
         name: 'command_name'
     },
     ....
}

Upvotes: 2

Vincent Gonnet
Vincent Gonnet

Reputation: 148

Try replacing command.data.name by command.name.

Your code should work if you have this :

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

Upvotes: 2

Related Questions