Docs · Running your bot

JavaScript bots: discord.js and Node.js

All docs

What is different for Node.js. Everything else in the docs applies to JavaScript bots as well.

Versions

Node.js 20, 22 and 24, chosen at checkout or later under Startup, Docker Image. Node 22 is the default. discord.js 14 needs Node 18 or newer, so any of them works.

A server can switch between Python and Node.js on the same page, so a Python server can become a JavaScript one without buying anything.

How your bot is started

With Bot file empty:

  1. If package.json has a start script, it runs npm start.
  2. Otherwise, the file named in "main".
  3. Otherwise the first of index.js, bot.js, main.js, app.js, src/index.js, src/bot.js, dist/index.js, index.mjs, src/index.mjs, index.ts, src/index.ts that exists.

To choose yourself, set Bot file to a file (src/index.js) or a script (npm run prod). Scripts that start a file watcher such as nodemon are meant for your computer; point it at the real start command instead.

Packages

Installed from package.json with npm, or with yarn or pnpm if their lock file is there, only when those files changed since the last start. Installing packages.

ES modules or require

Both work. If your code uses import, put "type": "module" in package.json (or end the files in .mjs). Node 20, 22 and 24 run it without that line too, but print a MODULE_TYPELESS_PACKAGE_JSON warning on every start and read each file twice. Current Node versions can require() most packages that only support import. If you still get ERR_REQUIRE_ESM (packages that use top-level await do this), switch that file to import, or install the older version of the package.

TypeScript

  • Compile it: a start script like "start": "tsc && node dist/index.js". Development dependencies are installed, so typescript can stay in devDependencies.
  • Or run it directly on Node 22 or 24, which can run .ts files that only use type annotations. Set Bot file to src/index.ts. Code using enum or path aliases still needs compiling.

The token and other secrets

client.login(process.env.BOT_TOKEN);

For other secrets use a .env file with the dotenv package. Keep node --env-file out of your start script: if the file is ever missing, Node stops before your bot starts. Startup settings.

Memory

Node's heap is capped at three quarters of your plan's memory (768 MB on Flurry), leaving room for everything else. A leaking bot then stops with a readable JavaScript heap out of memory instead of being killed without a message. discord.js keeps caches of messages and members; if memory climbs, limit them with makeCache and sweepers in the client options.

Voice and audio

ffmpeg is installed, and so are the build tools for native packages like @discordjs/opus. @discordjs/voice works as it does on your computer.

Something here wrong or out of date? Tell us and it gets fixed.