Bun Runtime
Build and run JavaScript/TypeScript applications with Bun — the all-in-one toolkit.
Quick Start
# Install Bun
powershell -c "irm bun.sh/install.ps1 | iex"
# Create a new project
bun init
# Run TypeScript directly (no ts-node needed)
bun run server.ts
# Package scripts (3-5x faster than npm)
bun install
bun add express
bun add -d typescript
# Run package.json scripts
bun run dev
bun run build
# Test runner (Jest-compatible)
bun test
# Bun's built-in bundler
bun build ./src/index.ts --outdir=./dist
// bun自带 features
// Fetch API (built-in, no polyfill needed)
const response = await fetch("https://api.example.com/data");
const data = await response.json();
// SQLite (built-in)
import { Database } from "bun:sqlite";
const db = new Database(":memory:");
db.run("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)");
db.run("INSERT INTO users (name) VALUES ($name)", { name: "Alice" });
const users = db.query("SELECT * FROM users").all();
// File I/O (Bun native)
const file = Bun.file("data.json");
const contents = await file.json();
// Environment variables (built-in)
const apiKey = Bun.env.API_KEY;
// WebSocket server
Bun.serve({
port: 3000,
fetch(req, server) {
if (server.upgrade(req)) return; // upgrade to WebSocket
return new Response("Hello");
},
websocket: {
message(ws, message) {
ws.send(`Echo: ${message}`);
}
}
});
Key Concepts
Bun is a JavaScript runtime, bundler, test runner, and package manager in one. Uses JavaScriptCore (not V8), starts faster than Node.js, and is fully compatible with Node.js APIs.
When to Use
- New TypeScript/JavaScript projects
- CI/CD pipelines needing faster installs and builds
- Development servers requiring hot reload
- Projects wanting built-in SQLite, fetch, and WebSocket support
Step-by-Step
- Install: run the one-line installer (
irm bun.sh/install.ps1 | iexon Windows) and verifybun --version. - Scaffold:
bun initcreatespackage.json,tsconfig.json, and entryindex.ts. - Write code natively: use built-in
fetch,Bun.file,bun:sqlite, andBun.serve— no polyfills, no framework. - Manage deps fast:
bun install; install exact versions withbun add <pkg>@<version>. - Run and test:
bun run dev(watch mode),bun test(Jest-compatible API), debug withbun --inspect. - Bundle and ship:
bun build ./src/index.ts --outdir=./dist --minify --target=bunfor a deployment artifact.
Examples
// REST API with built-in SQLite and typed routes
import { Database } from "bun:sqlite";
const db = new Database(":memory:");
db.run("CREATE TABLE todos (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, done INTEGER DEFAULT 0)");
Bun.serve({
port: 3000,
async fetch(req: Request) {
const url = new URL(req.url);
if (req.method === "POST" && url.pathname === "/todos") {
const { title } = await req.json();
const res = db.query("INSERT INTO todos (title) VALUES (?) RETURNING *").get(title);
return Response.json(res, { status: 201 });
}
if (url.pathname === "/todos") {
return Response.json(db.query("SELECT * FROM todos").all());
}
return Response.json({ error: "not found" }, { status: 404 });
},
});
# Serve the built SPA/SSR artifact and run tests
bun run --watch src/index.ts
bun test
bun build src/index.ts --outdir=dist --minify --target=bun && bun dist/index.js
Validation
bun --versionshows installed versionbun runexecutes TypeScript without compilation stepbun installis faster than npm/pnpm on the same projectbun testruns existing Jest/Vitest test suites