TL;DR
What: All-in-one JavaScript runtime, bundler, test runner, and package manager.
Why: 4x faster than Node.js, native TypeScript, built-in bundler, drop-in Node replacement.
Quick Start
Install:
# macOS/Linux
curl -fsSL https://bun.sh/install | bash
# Windows
powershell -c "irm bun.sh/install.ps1 | iex"
# Check version
bun --version
Run scripts:
bun run index.ts # Run TypeScript directly
bun run index.js # Run JavaScript
bun run dev # Run package.json script
Cheatsheet
| Command | Description |
|---|---|
bun run file | Run file |
bun install | Install dependencies |
bun add pkg | Add dependency |
bun remove pkg | Remove package |
bun test | Run tests |
bun build | Bundle for production |
bun init | Create new project |
Gotchas
Package management
# Initialize project
bun init
# Install dependencies
bun install
# Add package
bun add express
# Add dev dependency
bun add -d typescript
# Remove package
bun remove lodash
# Update packages
bun update
Running scripts
# Run TypeScript directly
bun run app.ts
# Run with watch mode
bun --watch run app.ts
# Run package.json script
bun run dev
# Run script (shorthand)
bun dev
HTTP server
// server.ts
const server = Bun.serve({
port: 3000,
fetch(request) {
const url = new URL(request.url);
if (url.pathname === "/") {
return new Response("Hello Bun!");
}
if (url.pathname === "/json") {
return Response.json({ message: "Hello" });
}
return new Response("Not Found", { status: 404 });
},
});
console.log(`Listening on http://localhost:${server.port}`);
File I/O
// Read file
const file = Bun.file("./data.json");
const text = await file.text();
const json = await file.json();
// Write file
await Bun.write("./output.txt", "Hello World");
await Bun.write("./data.json", JSON.stringify({ key: "value" }));
// Check if exists
const exists = await Bun.file("./data.json").exists();
Testing
// math.test.ts
import { describe, expect, test } from "bun:test";
describe("math", () => {
test("2 + 2", () => {
expect(2 + 2).toBe(4);
});
test("async test", async () => {
const result = await Promise.resolve(42);
expect(result).toBe(42);
});
});
bun test
bun test --watch
Bundler
# Bundle for browser
bun build ./src/index.ts --outdir ./dist
# With minification
bun build ./src/index.ts --outdir ./dist --minify
# For Node.js
bun build ./src/index.ts --outdir ./dist --target node
// Programmatic bundling
const result = await Bun.build({
entrypoints: ['./src/index.ts'],
outdir: './dist',
minify: true,
splitting: true,
});
Next Steps
- Bun Documentation - Official docs
- Bun APIs - Built-in APIs
- Bun Discord - Community
- Bun GitHub - Source code