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
- Run
npm run buildon your computer. This createsdist/index.js. - Create a server from the Node.js template in the Schokr console.
- Upload
dist/,package.jsonand your lockfile with the file manager. You don't need to uploadsrc/, though it does no harm. Skipnode_modules. - Create
.envin the server root withDISCORD_TOKEN=.... - In your server's Startup settings, set the main file to
dist/index.js. - 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.jsonlacks"type": "module", or the reverse. Matchmoduleintsconfig.jsonwith yourpackage.jsonsetting. CommonJS output, as above, avoids this. - Path aliases don't resolve at runtime:
pathsintsconfig.jsononly 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.