Bun is an all-in-one JavaScript runtime and toolkit written in Zig. Its package manager installs dependencies up to 23x faster than npm by using a binary lockfile, a global package cache, and symlink-based node_modules. In 2026, Bun 1.x has become a serious alternative to the Node.js ecosystem for both development and production.
Installation and Basic Commands
Bun installs in seconds and works as a drop-in replacement for npm commands. It reads package.json, installs to node_modules, and supports all npm registry packages.
# Install Bun
curl -fsSL https://bun.sh/install | bash
# Verify installation
bun --version # 1.x.x
# Install dependencies (reads package.json)
bun install
# Install a specific package
bun add express
bun add -d typescript @types/node # dev dependency
bun add -g bunx # global install
# Remove a package
bun remove lodash
# Update all packages
bun update
# Run a script from package.json
bun run build
bun run dev
# Execute a TypeScript file directly (no transpile step)
bun run index.tsWorkspaces (Monorepo Support)
Bun supports npm-style workspaces for monorepos. Use --filter to run scripts across all workspaces, or target specific packages.
// package.json — monorepo root
{
"name": "my-monorepo",
"workspaces": [
"packages/*",
"apps/*"
],
"scripts": {
"dev": "bun run --filter '*' dev",
"build": "bun run --filter '*' build",
"test": "bun test"
}
}
// Run a script only in specific workspace
bun run --filter @myapp/web dev
// Install a dependency in a specific workspace
bun add react --cwd apps/web
// Link a local workspace package
// (Bun automatically resolves workspace: protocol)The Binary Lockfile (bun.lockb)
Bun uses a binary lockfile (bun.lockb) instead of JSON. It is dramatically faster to parse than package-lock.json or yarn.lock, which is why bun install is so fast.
# bun.lockb is a binary lockfile (much faster to parse than JSON)
# To view lockfile contents in human-readable form:
bun bun.lockb # prints as yarn.lock format
# To regenerate lockfile:
bun install --frozen-lockfile # CI: fails if lockfile is stale
# Migrating from npm/yarn/pnpm:
# Bun reads package-lock.json, yarn.lock, pnpm-lock.yaml
# to preserve existing versions on first install
# .gitignore for Bun:
# node_modules/
# *.lock (optional — commit bun.lockb for reproducible installs)Performance Benchmarks
Bun's package manager is substantially faster than all alternatives due to its native code (Zig), binary lockfile, parallel downloads, and global cache.
# Benchmark: installing react + 1400 packages
# (warm cache, MacBook M2 Pro)
npm install: 28.3s
yarn install: 14.1s
pnpm install: 9.4s
bun install: 1.2s # 23x faster than npm!
# Cold cache (first install, no node_modules)
npm install: 38.7s
bun install: 4.1s # 9x faster
# bun install uses:
# - Symlink-based node_modules (like pnpm)
# - Binary lockfile (bun.lockb) for fast parsing
# - Parallel downloads
# - Global package cache (~/.bun/install/cache)
# - Native code (written in Zig)bunx and Built-in Tools
bunx runs packages without downloading — it uses cached binaries. Bun also includes a built-in test runner (Jest-compatible), TypeScript runner, bundler, and .env loader.
# bunx — like npx but instant (no download delay)
bunx create-next-app@latest my-app
bunx prettier --write .
bunx eslint src/
# Run TypeScript directly
bun index.ts # works without ts-node or compilation
bun --watch server.ts # auto-restart on file changes
# Bun's built-in test runner (Jest-compatible API)
bun test
bun test --watch
bun test src/utils.test.ts
// Example test file
import { expect, test, describe } from 'bun:test';
describe('math', () => {
test('adds numbers', () => {
expect(1 + 2).toBe(3);
});
test('async operations', async () => {
const result = await fetch('https://api.example.com/data');
expect(result.ok).toBe(true);
});
});Scripts and Build Integration
Bun runs package.json scripts faster than npm/yarn because it avoids spinning up a new Node.js process. It also includes a built-in bundler that can replace esbuild for simple use cases.
// package.json scripts with Bun
{
"scripts": {
"dev": "bun --watch src/index.ts",
"build": "bun build src/index.ts --outdir dist --target node",
"bundle:web": "bun build src/app.ts --outdir public/js --target browser --minify",
"test": "bun test",
"lint": "bunx eslint src/",
"format": "bunx prettier --write src/"
}
}
// Bun's built-in bundler (replaces esbuild/webpack for simple cases)
// bun build src/index.ts --outdir dist --target node --minify
// Environment variables (Bun auto-loads .env files)
// No need for dotenv package!
console.log(process.env.DATABASE_URL); // works in Bun automaticallyBun vs npm vs pnpm vs Yarn
| Feature | Bun | npm | pnpm | Yarn |
|---|---|---|---|---|
| Install speed (warm) | 1.2s | 28.3s | 9.4s | 14.1s |
| Lockfile format | Binary | JSON | YAML | Custom text |
| node_modules style | Symlinks | Hoisted | Symlinks | Hoisted/PnP |
| Workspace support | Yes | Yes | Yes | Yes |
| Built-in TypeScript | Yes (native) | No | No | No |
| Built-in test runner | Yes (Jest API) | No | No | No |
| .env auto-loading | Yes | No | No | No |
Best Practices
- Commit bun.lockb to version control for reproducible installs. Use --frozen-lockfile in CI to catch lockfile drift.
- Use bun --watch for development — it is faster than nodemon or ts-node-dev and works with TypeScript natively.
- Migrate from npm scripts gradually: replace npm run with bun run and npm install with bun install first.
- For monorepos, Bun workspaces are simpler than Turborepo's configuration but pair well with it for task caching.
- Bun auto-loads .env, .env.local, .env.production — no dotenv package needed. Use this to simplify config.
Frequently Asked Questions
Is Bun production-ready in 2026?
Yes. Bun 1.0+ reached stability in 2023, and 1.x in 2024-2026 has full Node.js API compatibility. Major companies use it in production. Oven (the company behind Bun) publishes compatibility reports showing 99%+ Node.js API coverage.
Does Bun work with all npm packages?
Almost all packages work. Bun implements Node.js built-in modules (fs, path, crypto, http, etc.) and is compatible with the npm registry. Edge cases exist for packages that use native Node addons (.node files) — those require native compilation and may not work immediately.
Can I use Bun with Next.js?
Yes for the package manager — run bun install instead of npm install and use bun run dev. For the runtime, Next.js 14+ added experimental Bun support. The package manager works today without any issues.
How does bun.lockb differ from package-lock.json?
bun.lockb is a binary format (not human-readable) that stores resolved package versions, checksums, and download URLs. It parses in microseconds vs. milliseconds for JSON lockfiles. You can view its contents with the bun bun.lockb command.
Should I use Bun or pnpm for a new project?
Both are excellent choices. Bun is faster (especially for install) and includes a runtime, test runner, and bundler. pnpm has broader ecosystem adoption, better monorepo tooling with Changesets, and is more battle-tested in large enterprise monorepos. For greenfield projects, Bun's all-in-one approach is very appealing.