Bun 是一个用 Zig 编写的全能 JavaScript 运行时和工具集。其包管理器通过使用二进制锁文件、全局包缓存和基于符号链接的 node_modules,安装依赖比 npm 快多达 23 倍。
安装和基本命令
Bun 安装只需几秒钟,可作为 npm 命令的即插即用替代品。它读取 package.json,安装到 node_modules,并支持所有 npm 注册表包。
# 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.ts工作区(Monorepo 支持)
Bun 支持 npm 风格的 monorepo 工作区。使用 --filter 在所有工作区运行脚本,或针对特定包。
// 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)二进制锁文件(bun.lockb)
Bun 使用二进制锁文件(bun.lockb)而非 JSON。解析速度比 package-lock.json 或 yarn.lock 快得多,这也是 bun install 如此快速的原因。
# 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)性能基准测试
Bun 的包管理器因其原生代码(Zig)、二进制锁文件、并行下载和全局缓存而大幅领先于所有竞争对手。
# 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 和内置工具
bunx 无需下载即可运行包——使用缓存的二进制文件。Bun 还包含内置测试运行器(Jest 兼容)、TypeScript 运行器、打包器和 .env 加载器。
# 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);
});
});脚本和构建集成
Bun 比 npm/yarn 更快地运行 package.json 脚本,因为它避免了启动新的 Node.js 进程。它还包含一个内置打包器,可以替代 esbuild 用于简单的用例。
// 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 |
最佳实践
- 将 bun.lockb 提交到版本控制以实现可重现的安装。在 CI 中使用 --frozen-lockfile 来检测锁文件漂移。
- 开发时使用 bun --watch——比 nodemon 或 ts-node-dev 更快,且原生支持 TypeScript。
- 逐步从 npm 脚本迁移:先将 npm run 替换为 bun run,将 npm install 替换为 bun install。
- 对于 monorepo,Bun 工作区比 Turborepo 配置简单,但与后者配合用于任务缓存效果很好。
- Bun 自动加载 .env、.env.local、.env.production——无需 dotenv 包。使用此功能简化配置。
常见问题
2026 年 Bun 是否适合生产使用?
是的。Bun 1.0+ 于 2023 年达到稳定,1.x 在 2024-2026 年具有完整的 Node.js API 兼容性。许多主要公司在生产中使用它。
Bun 与所有 npm 包兼容吗?
几乎所有包都可以工作。边缘情况存在于使用原生 Node 插件(.node 文件)的包,这些包可能不会立即工作。
我可以在 Next.js 中使用 Bun 吗?
可以用于包管理器——运行 bun install 替代 npm install,使用 bun run dev。对于运行时,Next.js 14+ 添加了实验性 Bun 支持。
bun.lockb 与 package-lock.json 有何不同?
bun.lockb 是二进制格式(非人类可读),存储已解析的包版本、校验和和下载 URL。解析速度比 JSON 锁文件快数个数量级。
新项目应该使用 Bun 还是 pnpm?
两者都是极佳选择。Bun 更快(尤其是安装),并包含运行时、测试运行器和打包器。pnpm 有更广泛的生态系统采用,在大型企业 monorepo 中更经过实战检验。