NEW 📢 Black Friday: our launch offer ends 30 Nov. Up to 70% off your first month on every game!See plans
Black FridayUp to 70% off your first month, ends 30 NovEnds in 9d 03h 37mBlack FridayUp to 70% off your first month, ends 30 NovEnds in 9d 03h 37mBlack FridayUp to 70% off your first month, ends 30 NovEnds in 9d 03h 37m

How to run a TypeScript Discord bot

Discord bot hostingUpdated Oct 29, 20264 min read

On a Node.js Bot server, set the Entry file to your .ts file and start: the server runs it with tsx, so there is no build step. You can also compile to JavaScript yourself, or pick the Bun Bot or Deno Bot type, which run TypeScript natively.

Option 1: run the .ts file directly (Node.js Bot)

  1. In Files, upload your src folder, package.json and tsconfig.json. Leave out node_modules and dist. To upload folders, zip them, upload the zip, tick it and click Unzip.
  2. Open the Bot page, pane Code, and set Entry file to your start file, for example src/index.ts. Click Save.
  3. Paste your token on the Discord pane and press Start.

An entry file ending in .ts or .mts is started with tsx, which is already on the server: you do not need it in package.json. Every package your code imports must be there, because npm install runs from package.json, at the first start and whenever the file changes.

tsx does not check types. Your bot starts even with type errors; run npx tsc --noEmit on your computer to see them.

The How it runs card of a Node.js bot with Entry file set to src/index.ts.

A minimal TypeScript bot

src/index.ts

import { Client, Events, GatewayIntentBits } from 'discord.js';

const client = new Client({ intents: [GatewayIntentBits.Guilds] });

client.once(Events.ClientReady, (c) => {
  console.log(`Logged in as ${c.user.tag}`);
});

client.login(process.env.DISCORD_TOKEN);

package.json needs "discord.js": "^14" under dependencies.

Option 2: compile first, run the JavaScript

  1. On your computer run npx tsc. Your compiled files land in the outDir of tsconfig.json, for example dist.
  2. Upload dist and package.json.
  3. Set Entry file to dist/index.js.

This starts a little faster. Rebuild and upload again after each change.

Option 3: Bun or Deno

The Bun Bot type runs index.ts as is and installs package.json with Bun. The Deno Bot type runs main.ts and loads npm packages with npm: imports. Same plan, you only change the type: How to host a Bun Discord bot, How to host a Deno Discord bot.

Common problems

  • Cannot find module or ERR_MODULE_NOT_FOUND for a package: add it to package.json or to Packages on the Code pane, then restart. The panel shows A package your code needs is missing with Open Packages.
  • ERR_MODULE_NOT_FOUND for one of your own files in compiled JavaScript with "type": "module": Node needs the extension in relative imports. Write import { x } from './util.js' in your TypeScript, even though the source file is util.ts.
  • SyntaxError: Cannot use import statement outside a module when running compiled files: your output uses import but package.json has no "type": "module". Add it, or compile to CommonJS.
  • Entry file ends in .cts: rename it to .ts, or compile it first. Only .ts and .mts are started with tsx.

Related: How to host a Node.js Discord bot, How to make a Discord bot with discord.js.

Already hosting your bot somewhere else? Move it to us

OUR LATEST BLOGS