Skip to main content
Bun is an all-in-one JavaScript runtime and toolkit designed for speed. It includes a bundler, test runner, and package manager. Remix applications run natively on Bun with excellent performance.

Prerequisites

Server Setup

Create a server entry point:
server.ts
import { createRouter } from 'remix/fetch-router'
import { route } from 'remix/fetch-router/routes'

let routes = route({
  home: '/',
  about: '/about',
})

let router = createRouter()

router.map(routes, {
  actions: {
    home() {
      return new Response('Hello from Bun!')
    },
    about() {
      return new Response('About page')
    },
  },
})

Bun.serve({
  port: 3000,
  fetch: (request) => router.fetch(request),
})

console.log('Server running on http://localhost:3000')
Run your server:
bun run server.ts

Package Management

Install dependencies with Bun’s fast package manager:
bun install remix
Bun is compatible with npm packages and will use your package.json.

Development Mode

Run with auto-reload:
bun --watch server.ts
Or add a script to package.json:
{
  "scripts": {
    "dev": "bun --watch server.ts",
    "start": "bun server.ts"
  }
}

Production Build

Bun can bundle your application for production:
bun build server.ts --outdir=./dist --target=bun
Run the bundled version:
bun dist/server.js

Environment Variables

Bun automatically loads .env files:
.env
PORT=3000
DATABASE_URL=postgresql://user:password@localhost:5432/mydb
SESSION_SECRET=your-secret-key
Access in your code:
let port = process.env.PORT || 3000
let databaseUrl = process.env.DATABASE_URL
let sessionSecret = process.env.SESSION_SECRET

HTTPS Server

Enable HTTPS in production:
import { file } from 'bun'

Bun.serve({
  port: 443,
  tls: {
    cert: file('./certs/cert.pem'),
    key: file('./certs/key.pem'),
  },
  fetch: (request) => router.fetch(request),
})

WebSocket Support

Bun has excellent WebSocket support:
Bun.serve({
  port: 3000,
  fetch(request, server) {
    // Upgrade to WebSocket
    if (request.headers.get('upgrade') === 'websocket') {
      server.upgrade(request)
      return
    }
    return router.fetch(request)
  },
  websocket: {
    open(ws) {
      console.log('Client connected')
    },
    message(ws, message) {
      ws.send(`Echo: ${message}`)
    },
    close(ws) {
      console.log('Client disconnected')
    },
  },
})

SQLite with Bun

Bun includes built-in SQLite support:
import { Database } from 'bun:sqlite'

let db = new Database('mydb.sqlite')

// Create table
db.run('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)')

// Insert data
db.run('INSERT INTO users (name) VALUES (?)', ['Alice'])

// Query data
let users = db.query('SELECT * FROM users').all()
console.log(users)

Deployment Options

VPS/Dedicated Server

  1. Install Bun on your server:
    curl -fsSL https://bun.sh/install | bash
    
  2. Upload your application
  3. Install dependencies: bun install
  4. Start your server: bun server.ts

Docker Container

Create a Dockerfile:
FROM oven/bun:1
WORKDIR /app
COPY package.json bun.lockb ./
RUN bun install --production
COPY . .
EXPOSE 3000
CMD ["bun", "server.ts"]
Build and run:
docker build -t remix-bun .
docker run -p 3000:3000 remix-bun

Process Management

Use PM2 or systemd to manage your Bun process:
pm2 start "bun server.ts" --name remix-app

Performance Benefits

  • Fast startup: Bun starts 4x faster than Node.js
  • Fast runtime: Native code execution for web APIs
  • Fast package manager: Install packages 10-100x faster
  • Built-in bundler: No need for separate build tools

Testing

Bun includes a built-in test runner:
import { test, expect } from 'bun:test'

test('router responds', async () => {
  let response = await router.fetch(new Request('http://localhost/'))
  expect(response.status).toBe(200)
})
Run tests:
bun test

Best Practices

  • Use bun build for production bundles
  • Leverage built-in SQLite for local development
  • Take advantage of Bun’s fast package installation
  • Use bun --watch for development
  • Profile with bun --inspect

Runtime Agnostic

How Remix runs on any JavaScript runtime

Performance

Optimize your application

Build docs developers (and LLMs) love