100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace

Bun Runtime Cheat Sheet

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.

2 PagesIntermediateJan 15, 2026

CLI Essentials

Bun replaces node, npm, jest, and webpack/esbuild with one binary.

bash
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.

typescript
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.

typescript
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.

typescript
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
Pro Tip

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.

Was this cheat sheet helpful?

Explore Topics

#BunRuntime#BunRuntimeCheatSheet#WebDevelopment#Intermediate#CLIEssentials#Bun#Serve#HTTP#Networking#APIs#Testing#CheatSheet#SkillVeris
Advertisement
Sri Hayavadhana Info-Tech

Professional Web Designing Services

  • Responsive Websites
  • E-commerce Solutions
  • SEO Friendly Design
  • Fast & Secure
  • Support & Maintenance
Get a Free Quote

Share this Cheat Sheet