{"id":16338,"library":"discord.js","title":"discord.js","description":"Discord.js is a comprehensive, performant, and object-oriented JavaScript library designed for interacting with the Discord API. It simplifies bot development by abstracting complex API calls and WebSocket management, providing a rich set of classes and utilities. The current stable version is 14.26.3, and the library maintains a frequent release cadence, often pushing out minor bug fixes and new features to keep pace with Discord's evolving API, as evidenced by the rapid 14.26.x updates. Key differentiators include its extensive documentation, a large and active community, and a modular ecosystem (e.g., `@discordjs/rest`, `@discordjs/builders`, `@discordjs/voice`) that allows developers to integrate specific functionalities as needed. It supports both CommonJS and ES Modules, with modern usage heavily favoring ESM and TypeScript for type safety and modern JavaScript features.","status":"active","version":"14.26.3","language":"javascript","source_language":"en","source_url":"https://github.com/discordjs/discord.js","tags":["javascript","discord","api","bot","client","node","discordapp","typescript"],"install":[{"cmd":"npm install discord.js","lang":"bash","label":"npm"},{"cmd":"yarn add discord.js","lang":"bash","label":"yarn"},{"cmd":"pnpm add discord.js","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Commonly used for creating and managing application commands (slash commands) and message components like buttons and modals.","package":"@discordjs/builders","optional":true},{"reason":"Provides a robust REST API client for direct interaction with Discord's HTTP API, often used alongside the main Client for command deployment.","package":"@discordjs/rest","optional":true},{"reason":"Enables advanced voice communication features for bots, allowing them to join voice channels and play audio.","package":"@discordjs/voice","optional":true},{"reason":"Optional dependency for faster WebSocket data compression and inflation, improving performance.","package":"zlib-sync","optional":true},{"reason":"Optional dependency for a much faster WebSocket connection.","package":"bufferutil","optional":true}],"imports":[{"note":"While CommonJS `require` is still supported, ES Modules `import` is the recommended and modern approach. v14+ requires Node.js v18+ and strongly encourages ESM.","wrong":"const Discord = require('discord.js'); const client = new Discord.Client();","symbol":"Client","correct":"import { Client, Events, GatewayIntentBits } from 'discord.js';"},{"note":"Introduced in v13, made mandatory and refined in v14. Replaces `Intents.FLAGS` with a more explicit `GatewayIntentBits` enum for better readability and maintainability.","wrong":"import { Intents } from 'discord.js'; // Deprecated in v14; previously Intents.FLAGS.GUILDS.\nconst client = new Client({ intents: [Intents.FLAGS.GUILDS] });","symbol":"GatewayIntentBits","correct":"import { GatewayIntentBits } from 'discord.js';"},{"note":"In v14, event names like `message` and `interaction` were deprecated and replaced with `messageCreate` and `interactionCreate` (accessible via `Events.MessageCreate` and `Events.InteractionCreate`) for clarity.","wrong":"client.on('message', ...); // Deprecated event name.\nclient.on('interaction', ...); // Deprecated event name.","symbol":"Events","correct":"import { Events } from 'discord.js';"},{"note":"Partials allow the bot to process events for uncached data (e.g., old messages or channels). It's crucial to enable necessary partials in the Client constructor if handling events on partially cached or uncached entities.","wrong":"const client = new Client({ partials: ['MESSAGE', 'CHANNEL'] });","symbol":"Partials","correct":"import { Partials } from 'discord.js';"}],"quickstart":{"code":"import { Client, Events, GatewayIntentBits } from 'discord.js';\n\n// Create a new client instance with required intents and partials\n// Intents are crucial for specifying which events your bot wants to receive from Discord.\n// MessageContent is a privileged intent and must be enabled in the Discord Developer Portal.\nconst client = new Client({\n    intents: [\n        GatewayIntentBits.Guilds,\n        GatewayIntentBits.GuildMessages,\n        GatewayIntentBits.MessageContent // Required for reading message content in v14+\n    ],\n    partials: [\n        Partials.Message, \n        Partials.Channel, \n        Partials.Reaction // Example partials, enable as needed\n    ]\n});\n\n// When the client is ready, run this once.\n// The 'ready' event indicates that the bot has successfully logged in.\nclient.once(Events.ClientReady, readyClient => {\n    console.log(`Ready! Logged in as ${readyClient.user.tag}`);\n});\n\n// Listen for 'messageCreate' events to respond to messages\nclient.on(Events.MessageCreate, async message => {\n    // Ignore messages from other bots or messages without content\n    if (message.author.bot || !message.content) return;\n\n    if (message.content.toLowerCase() === 'ping') {\n        await message.reply('Pong!');\n    }\n\n    if (message.content.toLowerCase() === 'hello') {\n        await message.channel.send(`Hello, ${message.author.username}!`);\n    }\n});\n\n// Log in to Discord with your client's token\n// Make sure to set your bot token in an environment variable named DISCORD_TOKEN\n// or provide it directly (not recommended for production).\nclient.login(process.env.DISCORD_TOKEN ?? '').catch(console.error);","lang":"typescript","description":"This quickstart initializes a basic Discord bot, logs it in, and makes it respond to 'ping' and 'hello' messages in any channel it has access to. It demonstrates mandatory intents and best practices for token management."},"warnings":[{"fix":"Upgrade your Node.js environment to version 18.x or newer. For v14.26.2, Node.js 22.12.0 or newer is required.","message":"Discord.js v14 removed Node.js versions older than 16.9.0, now requiring Node.js 18.0.0 or newer (the official documentation for 14.26.2 specifies Node.js 22.12.0 or newer). This can break deployments on older Node.js environments.","severity":"breaking","affected_versions":">=14.0.0"},{"fix":"Specify all necessary `GatewayIntentBits` in the `Client` constructor. For privileged intents like `MessageContent`, `GuildMembers`, and `GuildPresences`, enable them in your bot's settings on the Discord Developer Portal.","message":"Gateway Intents became mandatory in discord.js v13 and were further refined in v14. Bots must explicitly declare which event categories they intend to receive from Discord. Failing to provide intents or enabling privileged intents without Discord Developer Portal approval will prevent the bot from logging in or receiving events.","severity":"breaking","affected_versions":">=13.0.0"},{"fix":"Update event listeners to use the new `Events` enum properties (e.g., `Events.MessageCreate`, `Events.InteractionCreate`). Refer to the v13 to v14 update guide for a full list of changes.","message":"Several core event names were changed in v14 for better clarity and consistency. For example, `client.on('message', ...)` became `client.on(Events.MessageCreate, ...)` and `client.on('interaction', ...)` became `client.on(Events.InteractionCreate, ...)`.","severity":"breaking","affected_versions":">=14.0.0"},{"fix":"Ensure Node.js v18+ is used. Adapt any direct interactions with `REST` internals, especially `rest.raw` and `parseResponse`, to handle web-compatible `Response` objects.","message":"Discord.js v14 introduced breaking changes to the REST API internals, including the use of global `fetch` which requires Node.js v18+. The `REST` module's `raw` method now returns a web-compatible `Response` object, and `parseResponse` operates on this new object.","severity":"breaking","affected_versions":">=14.0.0"},{"fix":"Add required `Partials` (e.g., `Partials.Message`, `Partials.Channel`, `Partials.User`) to the `Client` constructor's `partials` array. Also, ensure privileged intents are enabled for related data.","message":"Partials, which allow the bot to handle events from uncached data, are now explicit. If your bot needs to react to events from old messages, channels, or users that might not be in its cache, you must enable the relevant `Partials` in the `Client` constructor. Failure to do so will result in events not firing for uncached data.","severity":"gotcha","affected_versions":">=12.0.0"},{"fix":"Use ES module `import` syntax for `discord.js` and other dependencies. Ensure your scripts are correctly configured as ES modules or CommonJS modules as appropriate for your project setup.","message":"When running Node.js with `\"type\": \"module\"` in `package.json`, attempting to use `require()` for discord.js or other ESM-only modules will result in a `ReferenceError: require is not defined in ES module scope` error.","severity":"gotcha","affected_versions":">=14.0.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Provide an array of `GatewayIntentBits` to the `intents` property in the `Client` constructor. Remember to also enable privileged intents in the Discord Developer Portal if needed.","cause":"The bot's client was instantiated without specifying required Gateway Intents in its configuration.","error":"TypeError [CLIENT_MISSING_INTENTS]: Valid intents must be provided for the Client."},{"fix":"Verify the bot token in your code matches the token from the Discord Developer Portal. Regenerate the token on the portal if unsure, and ensure it's kept private.","cause":"The bot token provided in `client.login()` is incorrect, expired, regenerated, or revoked.","error":"Error [TOKEN_INVALID]: An invalid token was provided."},{"fix":"Replace `Intents.FLAGS.<INTENT_NAME>` with `GatewayIntentBits.<IntentName>` and ensure `GatewayIntentBits` is imported from `discord.js`.","cause":"In discord.js v14, `Intents.FLAGS` was replaced by `GatewayIntentBits`. Old code trying to access `Intents.FLAGS` will throw this error.","error":"ReferenceError: Intents is not defined"},{"fix":"Change your import statements from `const Discord = require('discord.js');` to `import * as Discord from 'discord.js';` or `import { Client } from 'discord.js';`. Alternatively, ensure your `package.json` does not have `\"type\": \"module\"` if you intend to use CommonJS.","cause":"Attempting to use `require()` in a JavaScript file that is being treated as an ES Module (e.g., due to `\"type\": \"module\"` in `package.json`).","error":"ReferenceError: require is not defined in ES module scope, you can use import instead"}],"ecosystem":"npm"}