Bun Runtime Cheat Sheet
Bun's built-in APIs for HTTP servers, file I/O, bundling, testing, and package management as a fast Node.js-compatible runtime.
CLI Essentials
Bun replaces node, npm, jest, and webpack/esbuild with one binary.
bun install # install deps (reads package.json, writes bun.lockb)bun add zod # add a dependencybun run dev # run a package.json scriptbun index.ts # run a TS/JS file directly, no build stepbun test # run the built-in test runnerbun build ./index.ts --outdir ./dist --target browserbun --hot server.ts # hot-reload a running server
Bun.serve HTTP Server
Zero-dependency, high-throughput HTTP server built into the runtime.
const server = Bun.serve({ port: 3000, fetch(req) { const url = new URL(req.url) if (url.pathname === "/") return new Response("Hello Bun!") if (url.pathname === "/json") { return Response.json({ ok: true }) } return new Response("Not Found", { status: 404 }) }, error(err) { return new Response(`Error: ${err.message}`, { status: 500 }) },})console.log(`Listening on http://localhost:${server.port}`)
Fast File I/O
Bun.file/Bun.write avoid Node's fs callback ceremony for common cases.
const file = Bun.file("data.json")const exists = await file.exists()const text = await file.text()const json = await file.json()await Bun.write("out.txt", "hello world")await Bun.write("copy.json", Bun.file("data.json")) // fast file copy// streamingconst stream = file.stream()
Built-in Test Runner
Jest-compatible API, no separate test framework install needed.
import { test, expect, describe, mock } from "bun:test"describe("add", () => { test("adds two numbers", () => { expect(1 + 2).toBe(3) }) test("mocked fetch", () => { const fetchMock = mock(() => Promise.resolve({ ok: true })) expect(fetchMock).not.toHaveBeenCalled() })})// run: bun test --coverage
Bun-Specific Globals & APIs
Runtime APIs that don't exist in plain Node.
- Bun.serve()- built-in HTTP/WebSocket server, faster than express on raw throughput
- Bun.file() / Bun.write()- lazy file references with a Blob-like read API
- Bun.$ (Shell)- tagged template for running shell commands cross-platform: `await $\`ls -la\``
- Bun.SQLite- built-in synchronous SQLite driver, no native module install
- bun:test- Jest-compatible test runner built into the binary
- bunfig.toml- project-level runtime/bundler configuration file
Bun is Node-API compatible for most packages, but native Node addons relying on obscure `node:` internals can still break — run `bun test` and your CI matrix against Bun before fully dropping Node from production, especially for anything using worker_threads or crypto edge cases.
Explore Topics
Professional Web Designing Services
- Responsive Websites
- E-commerce Solutions
- SEO Friendly Design
- Fast & Secure
- Support & Maintenance