Section titled Installing Node.js and discord.jsInstalling Node.js and discord.js

Section titled Installing Node.jsInstalling Node.js

To use discord.js, you'll need to install Node.js. discord.js v14 requires Node v16.9.0 or higher.

To check if you already have Node installed on your machine (e.g., if you're using a VPS), run node -v in your terminal. If it outputs v16.9.0 or higher, then you're good to go! Otherwise, continue reading.

Tip

On Windows, it's as simple as installing any other program. Download the latest version from the Node.js website, open the downloaded file, and follow the steps from the installer.

On macOS, either:

  • Download the latest version from the Node.js website, open the package installer, and follow the instructions
  • Use a package manager like Homebrew with the command brew install node

On Linux, you can consult this page to determine how you should install Node. Native package managers often default to outdated versions of Node, so make sure you follow the recommended approach for your chosen Linux distribution carefully.

Section titled Preparing the essentialsPreparing the essentials

To use discord.js, you'll need to install it via npm (Node's package manager). npm comes with every Node installation, so you don't have to worry about installing that. However, before you install anything, you should set up a new project folder.

Navigate to a suitable place on your machine and create a new folder named discord-bot (or whatever you want). Next you'll need to open your terminal.

Section titled Opening the terminalOpening the terminal

If you use Visual Studio Code, you can press Ctrl + ` (backtick) to open its integrated terminal.

Tip

On Windows, either:

  • Shift + Right-click inside your project directory and choose the "Open command window here" option
  • Press Win + R and run cmd.exe, and then cd into your project directory

On macOS, either:

  • Open Launchpad or Spotlight and search for "Terminal"
  • In your "Applications" folder, under "Utilities", open the Terminal app

On Linux, you can quickly open the terminal with Ctrl + Alt + T.

With the terminal open, run the node -v command to make sure you've successfully installed Node.js. If it outputs v16.9.0 or higher, great!

Section titled Initiating a project folderInitiating a project folder

npm
yarn
pnpm

npm init; npm pkg set type="module"

This is the next command you'll be running. This command creates a package.json file for you, which will keep track of the dependencies your project uses, as well as other info.

This command will ask you a sequence of questions–you should fill them out as you see fit. If you're not sure of something or want to skip it as a whole, leave it blank and press enter. Setting the package type as module tells Node that you'll be writing this project using ESM (ECMAScript modules), supporting the latest JavaScript syntax and features.

Once you're done with that, you're ready to install discord.js!

Section titled Installing discord.jsInstalling discord.js

Now that you've installed Node.js and know how to open your console and run commands, you can finally install discord.js! Run the following command in your terminal:

npm
yarn
pnpm

npm install discord.js

And that's it! With all the necessities installed, you're almost ready to start coding your bot.

Section titled Installing a linterInstalling a linter

While you are coding, it's possible to run into numerous syntax errors or code in an inconsistent style. You should install a linter to ease these troubles. While code editors generally can point out syntax errors, linters coerce your code into a specific style as defined by the configuration. While this is not required, it is advised.