Bun: One Tool for Running, Testing, and Bundling JavaScript
Bun is a fast all-in-one JavaScript toolkit — runtime, package manager, bundler, and test runner in a single binary.

Bun is a modern JavaScript runtime built from scratch with Zig and JavaScriptCore (the engine powering Safari). Its selling point: a single binary that replaces Node, npm, Webpack or esbuild, and Jest — all at once.
The four things Bun does:
- Runtime — run
.js,.ts,.jsx,.tsxfiles directly, no transpilation step needed - Package manager —
bun installreadspackage.jsonand installs dependencies much faster than npm or pnpm - Bundler —
bun buildbundles code for browsers or for Bun itself - Test runner —
bun testspeaks Jest'sdescribe/it/expectAPI, no config needed
Installation
curl -fsSL https://bun.sh/install | bash
# or on macOS
brew install oven-sh/bun/bunVerify:
bun --version # e.g. 1.1.42Bun's binary is a single file at ~/.bun/bin/bun. No separate compiler, no PATH gymnastics.
Running TypeScript Without a Build Step
Create a file greet.ts:
function greet(name: string): string {
return `Hello, ${name}!`;
}
console.log(greet("world"));Run it:
bun greet.ts
# Hello, world!No tsc, no ts-node, no tsx. Bun transpiles TypeScript in-process using its own transpiler. The important caveat: Bun does not type-check. It strips types and executes; run tsc --noEmit in CI for the type-check pass.
Bun as a Package Manager
bun install # install all deps (reads package.json + bun.lockb)
bun add zod # add a package
bun remove zod # remove a package
bunx create-next-app # run a package binary (like npx)bun install creates bun.lockb, a binary lockfile. The first install is typically 10–20× faster than npm on a cold cache because Bun writes to a global cache and hard-links files rather than copying them.
Bun is a drop-in replacement for npm scripts:
bun run dev
bun run build
bun run testThe Built-in Test Runner
Bun ships a Jest-compatible test runner with no dependencies to install.
// math.test.ts
import { describe, it, expect } from "bun:test";
function add(a: number, b: number): number {
return a + b;
}
describe("add", () => {
it("returns the sum of two numbers", () => {
expect(add(2, 3)).toBe(5);
});
it("handles negative numbers", () => {
expect(add(-1, 1)).toBe(0);
});
});bun test
# bun test v1.1.42
#
# math.test.ts:
# ✓ add > returns the sum of two numbers [0.07ms]
# ✓ add > handles negative numbers [0.02ms]
#
# 2 pass, 0 failbun test supports --watch, --coverage, and --bail. It discovers files matching *.test.ts, *.spec.ts, or files inside a __tests__ directory. If you are migrating from Jest, most tests run as-is.
HTTP Servers with Bun.serve()
Bun has a built-in HTTP server API:
// server.ts
const server = Bun.serve({
port: 3000,
fetch(req: Request): Response {
const url = new URL(req.url);
if (url.pathname === "/ping") {
return new Response("pong");
}
return new Response("Not found", { status: 404 });
},
});
console.log(`Listening on http://localhost:${server.port}`);bun server.ts
# Listening on http://localhost:3000The handler receives a standard Request and returns a standard Response — the same interfaces as the browser Fetch API and Cloudflare Workers. No Express dependency needed for simple cases.
For streaming responses:
fetch(req) {
const stream = new ReadableStream({
start(controller) {
controller.enqueue(new TextEncoder().encode("chunk 1\n"));
controller.enqueue(new TextEncoder().encode("chunk 2\n"));
controller.close();
},
});
return new Response(stream, {
headers: { "Content-Type": "text/plain" },
});
},Reading and Writing Files
const file = Bun.file("data.json");
const data = await file.json(); // Promise<unknown>
const text = await file.text(); // Promise<string>Bun.file() is lazy — no I/O happens until you call .json(), .text(), or .bytes(). For writes:
await Bun.write("output.txt", "hello world");
await Bun.write("copy.json", Bun.file("original.json")); // zero-copyBun also reads .env files automatically — no dotenv package needed.
Compatibility with Node.js
Bun implements Node's built-in modules: fs, path, crypto, events, http, https, os, stream, child_process, and more. Most npm packages that work on Node run on Bun without changes. Where you hit a gap, the official compatibility table at bun.sh/docs lists the exact status per module.
One thing to know: Bun supports both CommonJS (require) and ESM (import) in the same file without extra config, which removes one common class of migration headache.
When to Use Bun
| Scenario | Recommendation |
|---|---|
| New internal CLI or script | Start with Bun from day one |
| New API or backend service | Bun.serve() is a solid choice; check your npm deps first |
| Existing Node project | Swap npm install → bun install as a quick win; migrate the runtime gradually |
| Frontend bundling | Vite is still the better choice for browser projects; bun build shines for server-side bundles |
| Shared library targeting Node | Stick with Node; Bun compatibility edge cases exist |
Running an Existing Next.js Project with Bun
Next.js 14+ works with Bun as the package manager. Replace npm with Bun and that's often the full migration:
# in an existing Next.js project
bun install
bun run dev
bun run buildThe Next.js framework itself still runs on Node.js for the dev server and build; Bun handles package installation and script invocation. Full Bun runtime support for Next.js is an ongoing project.
Summary
Bun consolidates four categories of tooling — runtime, package manager, bundler, test runner — into one fast binary. The TypeScript-first design removes most of the friction from the standard JavaScript toolchain. For new projects, starting with Bun is low-risk. For existing projects, swapping npm install for bun install alone saves a meaningful amount of time with zero other changes required.