Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

596
Vistas
ReferenceError: Discord is not defined, at new Discord.Collection

I have been following a tutorial to create a Discord bot by CodeLyon. I am up to the point of organizing commands into files. The 'ping' command worked when I was using the basic method in the tutorial, but after trying to use this method my command prompt threw this error when I tried to run it:

client.commands = new Discord.Collection();
                      ^

    ReferenceError: Discord is not defined
        at Object.<anonymous> (C:\Users\name\OneDrive\Desktop\VSC 
    Project 1\main.js:10:23)
←[90m    at Module._compile (node:internal/modules/cjs/loader:1101:14)←[39m
←[90m    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)←[39m
←[90m    at Module.load (node:internal/modules/cjs/loader:981:32)←[39m
←[90m    at Function.Module._load (node:internal/modules/cjs/loader:822:12)←[39m
←[90m    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)←[39m
←[90m    at node:internal/main/run_main_module:17:47←[39m

I have definitely installed node.js and discord.js, which were issues I saw other people had. However looking around I didn't see anyone with quite my issue so I decided to ask. I am pretty new to applying JavaScript but I know the syntax and some of the basics so I don't think it's that after reading through a few times.
Here is the general area where the error was thrown, it starts at line 1 and the error is at line 10.
const { Client, Intents, DiscordAPIError } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
// Lines 1 & 2 were changed from original tutorial in order to fix the Intents not defined error

const prefix = '-';

const fs = require('fs');


client.commands = new Discord.Collection(); // * Error is thrown here *


const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
    const command = require(`./commands/${file}`);

    client.commands.set(command.name, command);
}

I can share any of the other code if it's needed. Any help appreciated, since I don't know what's wrong :)

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

This was caused as you have not required your package discord.js as to a single constant Discord, you see every package is a module which has various properties and functions as displayed below:

components of discord.js


At the very top of your file you are destructuring these components from the module discord.js so you very well need to destructure the Collection too! like so:

    const { Client, Intents, DiscordAPIError, Collection } = require('discord.js');     
    // then you may define it like so    
    client.commands = new Collection()    

This would be absolutely valid as you can see here: display

about 4 years ago · Juan Pablo Isaza Denunciar

0

add this at bottom of your code

const Discord = require('discord.js');
about 4 years ago · Juan Pablo Isaza Denunciar

0

Yup that's autocorrect that made that problem. You should delete DiscordAPIError on Line 1 and make a new line right below saying const Discord = require('discord-js')

This way each time you say say Discord it invokes discord.js

That will solve your problem!

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda