Registry / communication / discord.js

discord.js

JSON →
library1.0.7jsnpmunverified

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.

npm install discord.js
INSTALL
IMPORT
SIG · DISCORD.JS
D
discord.js
communicationjavascriptv1.0.7
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Client
✓ import { Client, Events, GatewayIntentBits } from 'discord.js';
✗ const Discord = require('discord.js'); const client = new Discord.Client();
While CommonJS `require` is still supported, ES Modules `import` is the recommended and modern approach. v14+ requires Node.js v18+ and strongly encourages ESM.
GatewayIntentBits
✓ import { GatewayIntentBits } from 'discord.js';
✗ import { Intents } from 'discord.js'; // Deprecated in v14; previously Intents.FLAGS.GUILDS. const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
Introduced in v13, made mandatory and refined in v14. Replaces `Intents.FLAGS` with a more explicit `GatewayIntentBits` enum for better readability and maintainability.
Events
✓ import { Events } from 'discord.js';
✗ client.on('message', ...); // Deprecated event name. client.on('interaction', ...); // Deprecated event name.
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.
Partials
✓ import { Partials } from 'discord.js';
✗ const client = new Client({ partials: ['MESSAGE', 'CHANNEL'] });
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.

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.

import { Client, Events, GatewayIntentBits } from 'discord.js'; // Create a new client instance with required intents and partials // Intents are crucial for specifying which events your bot wants to receive from Discord. // MessageContent is a privileged intent and must be enabled in the Discord Developer Portal. const client = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent // Required for reading message content in v14+ ], partials: [ Partials.Message, Partials.Channel, Partials.Reaction // Example partials, enable as needed ] }); // When the client is ready, run this once. // The 'ready' event indicates that the bot has successfully logged in. client.once(Events.ClientReady, readyClient => { console.log(`Ready! Logged in as ${readyClient.user.tag}`); }); // Listen for 'messageCreate' events to respond to messages client.on(Events.MessageCreate, async message => { // Ignore messages from other bots or messages without content if (message.author.bot || !message.content) return; if (message.content.toLowerCase() === 'ping') { await message.reply('Pong!'); } if (message.content.toLowerCase() === 'hello') { await message.channel.send(`Hello, ${message.author.username}!`); } }); // Log in to Discord with your client's token // Make sure to set your bot token in an environment variable named DISCORD_TOKEN // or provide it directly (not recommended for production). client.login(process.env.DISCORD_TOKEN ?? '').catch(console.error);
Debug
Known issues
breakingDiscord.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.
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.
affects: >=14.0.0
breakingGateway 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.
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.
affects: >=13.0.0
breakingSeveral 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, ...)`.
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.
affects: >=14.0.0
breakingDiscord.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.
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.
affects: >=14.0.0
gotchaPartials, 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.
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.
affects: >=12.0.0
gotchaWhen 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.
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.
affects: >=14.0.0
Errors
Common errors & fixes
TypeError [CLIENT_MISSING_INTENTS]: Valid intents must be provided for the Client.
The bot's client was instantiated without specifying required Gateway Intents in its configuration.
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.
Error [TOKEN_INVALID]: An invalid token was provided.
The bot token provided in `client.login()` is incorrect, expired, regenerated, or revoked.
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.
ReferenceError: Intents is not defined
In discord.js v14, `Intents.FLAGS` was replaced by `GatewayIntentBits`. Old code trying to access `Intents.FLAGS` will throw this error.
fix
Replace `Intents.FLAGS.<INTENT_NAME>` with `GatewayIntentBits.<IntentName>` and ensure `GatewayIntentBits` is imported from `discord.js`.
ReferenceError: require is not defined in ES module scope, you can use import instead
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`).
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.
Upgrade
Version history
1.0.7latest on npm
Audit
Dependencies
@discordjs/buildersoptionalCommonly used for creating and managing application commands (slash commands) and message components like buttons and modals.
@discordjs/restoptionalProvides a robust REST API client for direct interaction with Discord's HTTP API, often used alongside the main Client for command deployment.
@discordjs/voiceoptionalEnables advanced voice communication features for bots, allowing them to join voice channels and play audio.
zlib-syncoptionalOptional dependency for faster WebSocket data compression and inflation, improving performance.
bufferutiloptionalOptional dependency for a much faster WebSocket connection.
Agent activity
3 hits · last 30 days
node
2
OpenAI (training)
1
Resources