Bun with Claude Code
Learn about Bun using Claude Code. Practical tips and code examples included.
Accelerating Bun Adoption With Claude Code
Bun is an all-in-one runtime for JavaScript/TypeScript. It bundles a package manager, bundler, and test runner, and runs several times faster than Node.js. Let’s use Claude Code to efficiently adopt Bun’s powerful features.
Starting a Project
> Create a new web app project with Bun.
> Include TypeScript, the Hono framework, and a test setup.
bun init my-app
cd my-app
bun add hono
// src/index.ts
import { Hono } from 'hono';
import { cors } from 'hono/cors';
import { logger } from 'hono/logger';
const app = new Hono();
app.use('*', logger());
app.use('/api/*', cors());
app.get('/api/health', (c) => {
return c.json({ status: 'ok', runtime: 'bun' });
});
app.get('/api/users/:id', async (c) => {
const id = c.req.param('id');
const user = await getUser(id);
return c.json(user);
});
export default {
port: 3000,
fetch: app.fetch,
};
Leveraging Bun’s Built-in APIs
> Implement a simple data store using Bun's file APIs and SQLite.
// src/db.ts
import { Database } from 'bun:sqlite';
const db = new Database('app.db', { create: true });
db.run(`
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
email TEXT UNIQUE NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
`);
export const createUser = db.prepare(
'INSERT INTO users (name, email) VALUES ($name, $email)'
);
export const getUser = db.prepare(
'SELECT * FROM users WHERE id = $id'
);
// Bun file operations
export async function saveUpload(file: File) {
const path = `./uploads/${file.name}`;
await Bun.write(path, file);
return path;
}
export async function readConfig() {
const file = Bun.file('./config.json');
return await file.json();
}
Running Tests
Bun ships with a fast built-in test runner.
> Write tests for the API endpoints.
> Use Bun's test runner.
// src/index.test.ts
import { describe, expect, it, beforeAll, afterAll } from 'bun:test';
describe('API endpoints', () => {
it('GET /api/health returns ok', async () => {
const res = await fetch('http://localhost:3000/api/health');
const data = await res.json();
expect(res.status).toBe(200);
expect(data.status).toBe('ok');
});
it('GET /api/users/:id returns user', async () => {
const res = await fetch('http://localhost:3000/api/users/1');
expect(res.status).toBe(200);
const user = await res.json();
expect(user).toHaveProperty('name');
});
});
# Run tests
bun test
# Watch mode
bun test --watch
# Coverage
bun test --coverage
Bundling and Building
> Bundle the front-end code with Bun.
> Enable tree shaking and minification.
// build.ts
await Bun.build({
entrypoints: ['./src/client/index.tsx'],
outdir: './dist',
target: 'browser',
minify: true,
splitting: true,
sourcemap: 'external',
define: {
'process.env.NODE_ENV': '"production"',
},
});
console.log('Build complete!');
Summary
With its blazing speed and integrated toolchain, Bun can significantly improve developer experience. Combine it with Claude Code to rapidly get up to speed on Bun-specific APIs and patterns. See also the API development guide and testing strategies.
For details about Bun, see the official Bun documentation.
Level up your Claude Code workflow
50 battle-tested prompt templates you can copy-paste into Claude Code right now.
Free PDF: Claude Code Cheatsheet in 5 Minutes
Key commands, shortcuts, and prompt examples on a single printable page.
About the Author
Masa
Engineer obsessed with Claude Code. Runs claudecode-lab.com, a 10-language tech media with 2,000+ pages.
Related Posts
How to Supercharge Your Side Projects with Claude Code [With Examples]
How to Supercharge Your Side Projects with Claude Code [With Examples]. A practical guide with code examples.
How to Automate Refactoring with Claude Code
Learn how to automate refactoring using Claude Code. Includes practical code examples and step-by-step guidance.
Complete CORS Configuration Guide with Claude Code
A complete CORS configuration guide using Claude Code. Practical tips and code examples included.