Discord bots2 minutes to read

How do I host a TypeScript discord.js bot?

Compile a TypeScript Discord bot on the host or before upload, start the JavaScript in dist, and avoid the usual tsconfig and ESM startup errors.

Build it to JavaScript, then run the output

Node.js runs JavaScript, so compile the TypeScript first and start the file it emits. On Bothosting, choose the discord.js profile, upload your source with package.json, the lockfile and tsconfig.json, then set RUN_COMMAND to install, build and start. Save the token as BOT_TOKEN. A small bot runs on the free Spark plan.

Set up the package scripts

package.json (excerpt)
"scripts": {
  "build": "tsc",
  "start": "node dist/index.js"
}
Variables · RUN_COMMAND
npm ci --include=dev && npm run build && npm start

--include=dev installs TypeScript when it is in devDependencies. If compiling uses too much memory on a small plan, build on your machine, upload dist, and use npm ci --omit=dev && npm start instead.

Fix the common startup errors

  • Cannot find module dist/index.js. Check outDir and rootDir. With source in src and no rootDir, output can land in dist/src/index.js.
  • Cannot use import statement outside a module. Make the module settings agree: "type": "module" in package.json with "module": "NodeNext" in tsconfig, or CommonJS in both. See npm ci failures for lockfile issues.
  • ts-node in production. It works but uses more memory and starts slower. Prefer compiled output.

Check the build that is running

Log a version string on ready and change it with each release. After a push or upload, look for the new string in Console, then run a slash command. To deploy on every push, connect the GitHub branch. For first-time setup, see deploying your first Discord bot.