NewSchokr 13 is here. A whole new UI, seven locations, snapshots and more. Read more →

How to host a TypeScript Discord bot on Schokr

Deploy a TypeScript discord.js bot on Schokr: compile to dist with tsc and run it on the Node.js template, or run the .ts files directly with Bun.

Updated · 2 min read

Node.js runs JavaScript, so a TypeScript bot needs to be compiled first. Build it to a dist folder with tsc, upload the output, and point your server's Startup settings at dist/index.js. Alternatively, use Schokr's Bun template, which runs .ts files directly.

Option 1: Build, then run on Node.js

Set up the project

npm init -y
npm install discord.js dotenv
npm install --save-dev typescript @types/node
npx tsc --init

A tsconfig.json that works well for a bot:

{
  "compilerOptions": {
    "target": "ES2022",
    "module": "commonjs",
    "rootDir": "src",
    "outDir": "dist",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true
  },
  "include": ["src"]
}

Write src/index.ts

import 'dotenv/config';
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);

Add scripts

{
  "main": "dist/index.js",
  "scripts": {
    "build": "tsc",
    "start": "node dist/index.js"
  }
}

Deploy

  1. Run npm run build on your computer. This creates dist/index.js.
  2. Create a server from the Node.js template in the Schokr console.
  3. Upload dist/, package.json and your lockfile with the file manager. You don't need to upload src/, though it does no harm. Skip node_modules.
  4. Create .env in the server root with DISCORD_TOKEN=....
  5. In your server's Startup settings, set the main file to dist/index.js.
  6. Start the server.

Every time you change code, rebuild locally and upload the new dist folder, then restart.

Why build locally?

typescript and @types/* are dev dependencies. Building on your computer keeps the server lean: it only installs what the compiled code needs at runtime. If you'd rather build on the server, move typescript into dependencies and set things up so tsc runs before start, but the local build is simpler.

Option 2: Run TypeScript directly with Bun

The Bun template runs .ts files without a build step and loads .env automatically. Set the main file to src/index.ts and start. Bun doesn't type-check, so run tsc --noEmit locally to catch type errors. See running a bot with Bun.

Common problems

  • "Cannot find module dist/index.js": the build wasn't uploaded, or the main file path is wrong. Paths are case-sensitive on the server.
  • "Cannot use import statement outside a module": you compiled to ES modules but package.json lacks "type": "module", or the reverse. Match module in tsconfig.json with your package.json setting. CommonJS output, as above, avoids this.
  • Path aliases don't resolve at runtime: paths in tsconfig.json only affects type-checking. Use relative imports or a bundler.
  • Commands folder not found: if you load command files dynamically, make sure the code looks in dist, and that the files there end in .js, not .ts.

See module not found errors for more.