bun
Bun runtime, package manager, bundler, and test runner. Use when running scripts with bun, managing packages, serving HTTP with Bun.serve, querying databases with Bun.sql/bun:sqlite/Bun.redis, shell scripting with $, using S3/file I/O, writing tests with bun:test, bundling or compiling to executable, or using any Bun-specific API (spawn, glob, semver, FFI, workers, plugins, HTMLRewriter).
Best use case
bun is best used when you need a repeatable AI agent workflow instead of a one-off prompt.
Bun runtime, package manager, bundler, and test runner. Use when running scripts with bun, managing packages, serving HTTP with Bun.serve, querying databases with Bun.sql/bun:sqlite/Bun.redis, shell scripting with $, using S3/file I/O, writing tests with bun:test, bundling or compiling to executable, or using any Bun-specific API (spawn, glob, semver, FFI, workers, plugins, HTMLRewriter).
Teams using bun should expect a more consistent output, faster repeated execution, less prompt rewriting.
When to use this skill
- You want a reusable workflow that can be run more than once with consistent structure.
When not to use this skill
- You only need a quick one-off answer and do not need a reusable workflow.
- You cannot install or maintain the underlying files, dependencies, or repository context.
Installation
Claude Code / Cursor / Codex
Manual Installation
- Download SKILL.md from GitHub
- Place it in
.claude/skills/bun/SKILL.mdinside your project - Restart your AI agent — it will auto-discover the skill
How bun Compares
| Feature / Agent | bun | Standard Approach |
|---|---|---|
| Platform Support | Not specified | Limited / Varies |
| Context Awareness | High | Baseline |
| Installation Complexity | Unknown | N/A |
Frequently Asked Questions
What does this skill do?
Bun runtime, package manager, bundler, and test runner. Use when running scripts with bun, managing packages, serving HTTP with Bun.serve, querying databases with Bun.sql/bun:sqlite/Bun.redis, shell scripting with $, using S3/file I/O, writing tests with bun:test, bundling or compiling to executable, or using any Bun-specific API (spawn, glob, semver, FFI, workers, plugins, HTMLRewriter).
Where can I find the source code?
You can find the source code on GitHub using the link provided at the top of the page.
SKILL.md Source
# Bun
## Quick Start
```bash
# Install
curl -fsSL https://bun.sh/install | bash
# Init project
bun init
# Run TypeScript directly
bun run index.ts
# Watch mode (hard restart on change)
bun --watch index.ts
# Hot reload (preserves global state, no restart)
bun --hot server.ts
```
## Package Management
```bash
bun install # install all deps
bun add express # add dependency
bun add -d @types/node # add dev dependency
bun remove lodash # remove
bun update # update all
bun update --latest # ignore semver ranges
bunx prettier --write . # execute package binary (npx equivalent)
bun patch express # patch a dependency in node_modules
```
### Workspaces
```json
// root package.json
{ "workspaces": ["packages/*"] }
// child package.json
{ "dependencies": { "shared": "workspace:*" } }
```
```bash
bun install --filter "pkg-*" # install filtered workspaces
```
### Environment Variables
`.env` files auto-loaded in order: `.env` → `.env.$(NODE_ENV)` → `.env.local`
```ts
Bun.env.API_KEY // Bun-native
process.env.API_KEY // Node.js compat
import.meta.env.API_KEY
```
```bash
bun --env-file=.env.staging run start
```
## HTTP Server
```ts
Bun.serve({
port: 3000,
routes: {
"/": new Response("Home"),
"/users/:id": (req) => Response.json({ id: req.params.id }),
"/api/posts": {
GET: () => Response.json([]),
POST: async (req) => Response.json(await req.json(), { status: 201 }),
},
"/api/*": Response.json({ error: "Not found" }, { status: 404 }),
"/favicon.ico": Bun.file("./favicon.ico"),
},
fetch(req) {
return new Response("Not Found", { status: 404 });
},
});
```
### WebSocket Upgrade
```ts
Bun.serve({
fetch(req, server) {
if (server.upgrade(req, { data: { userId: "123" } })) return;
return new Response("Not a WebSocket", { status: 400 });
},
websocket: {
open(ws) { ws.subscribe("chat"); },
message(ws, msg) { ws.publish("chat", msg); },
close(ws) {},
},
});
```
### Fullstack (HTML Imports)
```ts
import homepage from "./index.html";
Bun.serve({
routes: { "/": homepage },
development: true, // enables HMR
});
```
See [references/http-server.md](references/http-server.md) for cookies, static routes, TLS, server lifecycle, metrics.
## Databases
### Bun.sql (Postgres / MySQL / SQLite)
```ts
import { sql, SQL } from "bun";
// Auto-reads DATABASE_URL / POSTGRES_URL / MYSQL_URL
const users = await sql`SELECT * FROM users WHERE active = ${true}`;
// Explicit connection
const pg = new SQL("postgres://user:pass@localhost:5432/mydb");
const mysql = new SQL("mysql://user:pass@localhost:3306/mydb");
const sqlite = new SQL(":memory:");
// Insert with object helper
const [user] = await sql`INSERT INTO users ${sql({ name: "Alice", email: "a@b.com" })} RETURNING *`;
// Bulk insert
await sql`INSERT INTO users ${sql([user1, user2, user3])}`;
// Transactions
await sql.begin(async (tx) => {
const [u] = await tx`INSERT INTO users ${sql({ name: "Bob" })} RETURNING *`;
await tx`INSERT INTO accounts (user_id) VALUES (${u.id})`;
});
```
### bun:sqlite (Sync, Embedded)
```ts
import { Database } from "bun:sqlite";
const db = new Database("app.db");
db.run("CREATE TABLE IF NOT EXISTS kv (key TEXT PRIMARY KEY, val TEXT)");
const row = db.query("SELECT * FROM kv WHERE key = ?").get("foo");
const all = db.query("SELECT * FROM kv").all();
// Class mapping
class User { id!: number; name!: string; }
const users = db.query("SELECT * FROM users").as(User).all();
```
### Bun.redis
```ts
import { redis } from "bun";
await redis.set("key", "value", { ex: 60 });
const val = await redis.get("key");
await redis.del("key");
await redis.hmset("user:1", { name: "Alice", role: "admin" });
```
See [references/database.md](references/database.md) for transactions, savepoints, MySQL/SQLite specifics, Redis pub/sub, connection options.
## Shell ($)
```ts
import { $ } from "bun";
// Run and print to stdout
await $`echo "Hello"`;
// Capture output
const text = await $`ls -la`.text();
const data = await $`cat config.json`.json();
for await (const line of $`cat file.txt`.lines()) { }
// Pipes
await $`cat file.txt | grep "pattern" | wc -l`;
// Redirect from/to JS objects
await $`cat < ${new Response("data")} > ${Bun.file("out.txt")}`;
// Error handling
const { exitCode } = await $`may-fail`.nothrow().quiet();
// Config
$.cwd("/tmp");
$.env({ ...process.env, NODE_ENV: "production" });
```
**Security:** Interpolated variables are auto-escaped (no shell injection). Use `{ raw: str }` to bypass.
See [references/shell.md](references/shell.md) for builtins, command substitution, brace expansion.
## File I/O & S3
```ts
// Read
const file = Bun.file("data.json");
const json = await file.json(); // also: .text(), .bytes(), .stream(), .arrayBuffer()
console.log(file.size, file.type); // size in bytes, MIME type
// Write
await Bun.write("out.txt", "hello");
await Bun.write(Bun.file("copy.bin"), Bun.file("src.bin")); // copy file
// Incremental write
const writer = Bun.file("log.txt").writer();
writer.write("line 1\n");
writer.write("line 2\n");
writer.end();
// S3
import { s3 } from "bun";
const obj = s3.file("data.json");
const data = await obj.json();
await obj.write(JSON.stringify({ ok: true }));
const url = obj.presign({ expiresIn: 3600, method: "PUT" });
await obj.delete();
// s3:// protocol
const res = await fetch("s3://bucket/file.txt");
// Glob
const glob = new Bun.Glob("**/*.ts");
for await (const path of glob.scan(".")) console.log(path);
```
See [references/file-io.md](references/file-io.md) for S3 credentials, multipart uploads, streams, hashing, semver.
## Testing
```ts
import { test, expect, describe, mock, spyOn, beforeEach } from "bun:test";
describe("math", () => {
test("adds", () => expect(1 + 1).toBe(2));
test.each([
[1, 2, 3],
[4, 5, 9],
])("%i + %i = %i", (a, b, expected) => {
expect(a + b).toBe(expected);
});
test.skip("wip", () => {});
test.todo("implement later");
});
// Mock functions
const fn = mock(() => 42);
fn();
expect(fn).toHaveBeenCalled();
// Module mocking
mock.module("./db", () => ({
query: mock(() => []),
}));
// Spies
const spy = spyOn(console, "log");
console.log("test");
expect(spy).toHaveBeenCalledWith("test");
// Snapshots
test("snap", () => {
expect({ a: 1, b: "hello" }).toMatchSnapshot();
});
```
```bash
bun test # run all tests
bun test --watch # watch mode
bun test --coverage # with coverage
bun test --update-snapshots # update snapshots
bun test --bail # stop on first failure
bun test --test-name-pattern "add" # filter by name
```
See [references/testing.md](references/testing.md) for all matchers, lifecycle hooks, type testing, retry/repeats, DOM testing.
## Bundler & Compile
```ts
// Bundle for browser
const result = await Bun.build({
entrypoints: ["./src/index.ts"],
outdir: "./dist",
splitting: true,
minify: true,
sourcemap: "linked",
target: "browser", // or "bun", "node"
});
if (!result.success) {
for (const log of result.logs) console.error(log);
}
```
```bash
# Compile to standalone executable
bun build --compile --minify --sourcemap --bytecode ./app.ts --outfile myapp
# Cross-compile
bun build --compile --target=bun-linux-x64 ./app.ts --outfile myapp-linux
bun build --compile --target=bun-darwin-arm64 ./app.ts --outfile myapp-mac
```
```ts
// Embed files in executable
import icon from "./icon.png" with { type: "file" };
import db from "./data.db" with { type: "sqlite", embed: "true" };
```
See [references/bundler.md](references/bundler.md) for all build options, plugins, cross-compile targets, Windows options, Transpiler API.
## Child Processes
```ts
// Async
const proc = Bun.spawn(["ls", "-la"], {
cwd: "/tmp",
stdout: "pipe",
});
const output = await new Response(proc.stdout).text();
await proc.exited;
// Sync
const { stdout, exitCode } = Bun.spawnSync(["echo", "hi"]);
// Timeout + abort
const ctrl = new AbortController();
Bun.spawn(["sleep", "100"], { signal: ctrl.signal, timeout: 5000 });
// IPC between bun processes
const child = Bun.spawn(["bun", "worker.ts"], {
ipc(msg) { console.log("from child:", msg); },
});
child.send({ type: "start" });
```
## Configuration (bunfig.toml)
```toml
# Common options
preload = ["./setup.ts"]
logLevel = "warn"
[run]
shell = "bun" # use Bun's shell instead of system shell
bun = true # alias node to bun in scripts
[test]
preload = ["./test-setup.ts"]
coverage = true
coverageThreshold = 0.8
retry = 2
[install]
exact = true # pin exact versions
frozenLockfile = true # CI: fail if lockfile out of date
auto = "fallback" # auto-install missing packages
```
See [references/configuration.md](references/configuration.md) for full bunfig.toml reference.
## Reference Index
| Topic | Reference |
|-------|-----------|
| HTTP server, routing, WebSockets, cookies, fullstack | [references/http-server.md](references/http-server.md) |
| Bun.sql, bun:sqlite, Bun.redis | [references/database.md](references/database.md) |
| TCP, UDP, DNS, fetch | [references/networking.md](references/networking.md) |
| Bun Shell ($) | [references/shell.md](references/shell.md) |
| bun:test, mocking, snapshots, coverage | [references/testing.md](references/testing.md) |
| Bun.build, compile to executable, plugins | [references/bundler.md](references/bundler.md) |
| Bun.file, S3, Glob, streams, hashing, semver | [references/file-io.md](references/file-io.md) |
| bunfig.toml full reference | [references/configuration.md](references/configuration.md) |
| Workers, HTMLRewriter, FFI, C compiler, secrets, Node.js compat | [references/advanced.md](references/advanced.md) |Related Skills
solid
Apply SOLID principles to write flexible, maintainable, and testable code. Use when designing classes, interfaces, and module boundaries. Covers Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation, and Dependency Inversion with practical TypeScript examples and detection heuristics.
denji
Manage SVG icons as framework components using Denji CLI. Use when the user needs to add, remove, list, export, import, or manage SVG icons in React, Preact, Solid, Qwik, Vue, or Svelte projects. Triggers include requests to "add an icon", "set up icons", "manage SVG icons", "remove an icon", "list icons", "export icons", "import icons", "dry-run icon add", or any task involving Iconify icons as framework components.
zod
Zod 4 — TypeScript-first schema validation with static type inference. Use when writing Zod schemas, validating data, defining types with Zod, parsing input, creating form validation schemas, defining API request/response schemas, working with z.object, z.string, z.number, z.enum, z.array, z.union, z.discriminatedUnion, z.file, z.jwt, z.email, z.uuid, z.url, z.codec, z.toJSONSchema, z.fromJSONSchema, z.int, z.stringbool, z.templateLiteral, z.record, z.partialRecord, or any other Zod API. Also use when migrating from Zod 3 to Zod 4, or when the user's package.json shows zod@^4. CRITICAL: Always use Zod 4 APIs. Never use deprecated Zod 3 patterns unless user explicitly requests Zod 3 compatibility.
web-design-guidelines
Review UI code for Web Interface Guidelines compliance. Use when asked to "review my UI", "check accessibility", "audit design", "review UX", or "check my site against best practices".
vercel-react-best-practices
React and Next.js performance optimization guidelines from Vercel Engineering. This skill should be used when writing, reviewing, or refactoring React/Next.js code to ensure optimal performance patterns. Triggers on tasks involving React components, Next.js pages, data fetching, bundle optimization, or performance improvements.
vercel-composition-patterns
React composition patterns that scale. Use when refactoring components with boolean prop proliferation, building flexible component libraries, or designing reusable APIs. Triggers on tasks involving compound components, render props, context providers, or component architecture. Includes React 19 API changes.
turborepo
Turborepo monorepo build system guidance. Triggers on: turbo.json, task pipelines, dependsOn, caching, remote cache, the "turbo" CLI, --filter, --affected, CI optimization, environment variables, internal packages, monorepo structure/best practices, and boundaries. Use when user: configures tasks/workflows/pipelines, creates packages, sets up monorepo, shares code between apps, runs changed/affected packages, debugs cache, or has apps/packages directories.
tanstack-virtual
TanStack Virtual headless virtualization for React. Use when rendering large lists (100+ items), implementing virtual scroll, building infinite scroll feeds, virtualizing grids or tables, using window-level scrolling, or implementing masonry/lane layouts with @tanstack/react-virtual. Triggers on: useVirtualizer, useWindowVirtualizer, virtual list, virtual scroll, list virtualization.
tanstack-query
TanStack Query (React Query) v5 best practices for data fetching, caching, mutations, and server state management. Use when building data-driven React applications, setting up query configurations, implementing mutations/optimistic updates, configuring caching strategies, integrating with SSR, or fixing v4→v5 migration errors.
tanstack-pacer
TanStack Pacer best practices for execution control in React — debouncing, throttling, rate limiting, queuing, and batching. Use when implementing search inputs, scroll handlers, API rate limits, task queues, bulk operations, or any scenario requiring controlled execution timing with reactive state.
tanstack-hotkeys
Guide for implementing keyboard shortcuts in React using @tanstack/react-hotkeys. Use when building hotkey/shortcut features, registering keyboard shortcuts, handling key sequences, recording custom shortcuts, tracking held keys, or formatting hotkeys for display in React applications.
skill-creator
Guide for creating effective skills. This skill should be used when users want to create a new skill (or update an existing skill) that extends Claude's capabilities with specialized knowledge, workflows, or tool integrations.