Bun Cheat Sheet
Quick reference for Bun — runtime commands, package management, HTTP server, file I/O, testing, and bundler. All essential Bun patterns in one page.
CLI Commands
| bun run main.ts | Run TypeScript/JavaScript file |
| bun run dev | Run script from package.json |
| bun install | Install all dependencies (fast) |
| bun add zod | Add a dependency |
| bun add -d vitest | Add dev dependency |
| bun remove lodash | Remove a dependency |
| bun update | Update all dependencies |
| bun test | Run tests (built-in Jest-compatible runner) |
| bun build ./src/index.ts --outdir ./dist | Bundle for production |
| bun init | Initialize new project |
| bunx create-next-app | Run package binary (like npx) |
| bun --hot main.ts | Run with hot reloading |
HTTP Server
| Bun.serve({ fetch(req) { return new Response("Hi") } }) | Minimal HTTP server |
| Bun.serve({ port: 3000, fetch(req) { ... } }) | Server on custom port |
| new Response(JSON.stringify(data), { headers: { "Content-Type": "application/json" } }) | JSON response |
| new URL(req.url).pathname | Get request path |
| await req.json() | Parse JSON body |
| await req.formData() | Parse form data |
| req.method | Get HTTP method |
| Bun.serve({ fetch, websocket: { message(ws, msg) { ... } } }) | WebSocket server |
File I/O
| await Bun.file("data.txt").text() | Read text file |
| await Bun.file("data.json").json() | Read and parse JSON file |
| await Bun.write("out.txt", "content") | Write text file |
| await Bun.write("out.txt", Bun.file("in.txt")) | Copy file |
| Bun.file("img.png").size | Get file size |
| Bun.file("img.png").type | Get MIME type |
| const buf = await Bun.file("bin").arrayBuffer() | Read as ArrayBuffer |
| Bun.write(Bun.stdout, "hello\n") | Write to stdout |
Testing
| import { test, expect } from "bun:test" | Import test utilities |
| test("name", () => { expect(1 + 1).toBe(2) }) | Basic test |
| test("async", async () => { ... }) | Async test |
| expect(value).toBe(expected) | Strict equality |
| expect(value).toEqual(expected) | Deep equality |
| expect(() => fn()).toThrow() | Assert throws |
| test.skip("name", () => { ... }) | Skip test |
| bun test --coverage | Run with coverage |
Package Management
| bun install | Install deps (25x faster than npm) |
| bun.lockb | Binary lockfile (faster parsing) |
| bun add --trust pkg | Install with lifecycle scripts |
| bun pm cache rm | Clear package cache |
| bun pm ls | List installed packages |
| bun link | Link local package globally |
| bun patch pkg | Patch a dependency |
| // bunfig.toml: [install] registry = "..." | Custom registry config |
Built-in APIs
| Bun.password.hash("secret") | Hash a password (bcrypt/argon2) |
| Bun.password.verify("secret", hash) | Verify password hash |
| new Bun.CryptoHasher("sha256").update(data).digest("hex") | SHA-256 hash |
| Bun.sleep(1000) | Sleep for milliseconds |
| Bun.env.API_KEY | Read environment variable |
| Bun.which("git") | Find executable path |
| Bun.spawn(["ls", "-la"]) | Spawn subprocess |
| Bun.peek(promise) | Synchronously read resolved promise |
Step-by-Step Guide
Read Guide →