javascriptjsondiscord.jsto-json

discord.js: Cannot read properties of undefined (reading 'toJSON')


Hello,

I was trying to make a bot with discord.js, and following the tutorial at https://discordjs.guide.

And apparently data.toJSON() wasn't recognized (even though builders are installed)

deploy-commands.js:

const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const { token, clientId, guildId } = require('./config.json');
const fs = require('node:fs');

const commands = [];
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    commands.push(command.data.toJSON()); // error here
}

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

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

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

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

[enter image description here]


Solution

  • I had some empty files in my commands folder that were getting read without a data property. I deleted them to solve the problem.