NUOVO 📢 Risparmia fino al 30% sul tuo primo mese, offerta a tempo limitato!

How to run a TypeScript Discord bot

Discord bot hostingUpdated Oct 6, 20263 min read

You can run a TypeScript bot on a Node.js bot server in two ways.

Option 1: run the .ts file directly

Set Main file to your TypeScript file, for example src/index.ts, in Settings, Game Settings. A main file that ends in .ts is started with the tsx runner, so you do not need to compile. Upload your src folder, package.json and tsconfig.json. Put every package your code imports in package.json, including typescript and type packages if your code needs them.

This is the simplest option. Startup is a little slower than compiled JavaScript.

Option 2: compile first, run the JavaScript

  1. Build on your computer with npx tsc.
  2. Upload the compiled folder (for example dist) and package.json.
  3. Set Main file to dist/index.js.

This starts faster and uses a little less memory. Remember to rebuild and upload each time you change the code.

Common problems

  • Cannot find module: a package is missing from package.json. Add it, or add it to Extra npm packages.
  • ERR_MODULE_NOT_FOUND with ES modules: relative imports need the file extension when "type": "module" is set. Use ./util.js (the compiled name) in TypeScript imports.
  • SyntaxError: Cannot use import statement outside a module: your file uses import but Node treats it as CommonJS. Set "type": "module" in package.json, or compile to CommonJS.
  • Types missing at start-up do not stop tsx. Type errors only matter when you run tsc yourself.

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

CREA IL TUO SERVER DI GIOCO
PRONTO A INIZIARE?

Risparmia fino al 30% sul tuo nuovo server di gioco.
Prestazioni premium, attivazione immediata e affidabilità imbattibile a un prezzo migliore.

ULTIMI ARTICOLI