How to run a Discord bot with Bun on Schokr
Host a discord.js bot with the Bun runtime on Schokr: run TypeScript without a build step, use Bun's built-in .env loading and install with bun install.
Updated · 2 min read
Schokr has a Bun template, so you can run a JavaScript or TypeScript Discord bot with Bun instead of Node.js. Bun runs .ts files directly and loads .env files automatically, which makes small bots simpler to set up.
Why use Bun for a bot?
| Feature | Bun | Node.js |
|---|---|---|
| Run TypeScript | Directly, no build step | Needs a build (or a loader) |
Load .env | Automatic | Needs dotenv or --env-file |
| Install packages | bun install | npm install |
| Built-in SQLite | bun:sqlite | Third-party package or newer Node versions |
Most pure JavaScript packages, including discord.js, work on Bun. Packages with native add-ons or voice support may behave differently, so test locally first. If something misbehaves, the Node.js template is the safe fallback.
1. Set up the project locally
mkdir my-bot
cd my-bot
bun init -y
bun add discord.js
2. Write 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.on(Events.InteractionCreate, async (interaction) => {
if (!interaction.isChatInputCommand()) return;
if (interaction.commandName === 'ping') {
await interaction.reply('Pong from Bun!');
}
});
client.login(Bun.env.DISCORD_TOKEN);
Create .env next to it:
DISCORD_TOKEN=your-token-here
Bun reads .env on its own, so you don't need the dotenv package. process.env.DISCORD_TOKEN works too.
The /ping command must be registered before it appears in Discord. See slash commands in discord.js.
3. Add a start script
{
"name": "my-bot",
"module": "index.ts",
"type": "module",
"scripts": {
"start": "bun run index.ts"
},
"dependencies": {
"discord.js": "^14.16.0"
}
}
Run bun start locally and confirm the bot logs in.
4. Deploy to Schokr
- Create a server from the Bun template in the Schokr console.
- Upload
index.ts,package.json, your lockfile and any other source folders with the file manager. Skipnode_modules. - Create
.envin the server root with your token. - In your server's Startup settings, set the main file or startup command to your entry point, for example
index.ts. - Start the server. Dependencies from
package.jsonare installed, then your bot starts. Watch the console for the "Logged in" line.
Troubleshooting
- "Cannot find package": the package isn't in
package.json. Add it locally withbun add, upload the newpackage.jsonand restart. - Token is undefined:
.envmust be in the folder Bun runs from (the server root) and the variable name must match exactly. - A package works on Node but not Bun: check the package's issues and Bun's compatibility notes on bun.sh, or switch to the Node.js template.