Bun 包管理基础 #

概述 #

Bun 内置了高性能的包管理器,比 npm 快 25 倍以上。它完全兼容 npm 生态,可以直接使用 package.json 和 node_modules。

安装依赖 #

安装所有依赖 #

bash
# 安装 package.json 中的所有依赖
bun install

# 简写
bun i

安装选项 #

bash
# 生产模式(不安装 devDependencies)
bun install --production

# 离线模式
bun install --offline

# 忽略脚本
bun install --ignore-scripts

# 强制重新安装
bun install --force

# 冻结锁文件(CI 环境)
bun install --frozen-lockfile

添加依赖 #

生产依赖 #

bash
# 添加依赖
bun add react
bun add react react-dom

# 添加特定版本
bun add react@18
bun add react@18.2.0
bun add react@latest

开发依赖 #

bash
# 添加开发依赖
bun add -d typescript
bun add --dev @types/node
bun add -d eslint prettier

全局依赖 #

bash
# 添加全局依赖
bun add -g prettier
bun add --global eslint

# 查看全局安装位置
bun pm bin -g

其他选项 #

bash
# 精确版本(不使用 ^)
bun add --exact react

# 可选依赖
bun add -O package-name

# 从 Git 仓库安装
bun add github:user/repo
bun add git@github.com:user/repo.git
bun add https://github.com/user/repo.git

# 从本地路径安装
bun add ./local-package

# 从 tarball 安装
bun add ./package.tgz

移除依赖 #

bash
# 移除依赖
bun remove react

# 移除多个
bun remove react react-dom

# 移除全局依赖
bun remove -g prettier

更新依赖 #

bash
# 更新所有依赖
bun update

# 更新特定依赖
bun update react

# 交互式更新
bun update --interactive

package.json #

基本结构 #

json
{
  "name": "my-project",
  "version": "1.0.0",
  "type": "module",
  "scripts": {
    "start": "bun run src/index.ts",
    "dev": "bun --hot run src/index.ts",
    "test": "bun test",
    "build": "bun build src/index.ts --outdir dist"
  },
  "dependencies": {
    "react": "^18.2.0",
    "react-dom": "^18.2.0"
  },
  "devDependencies": {
    "typescript": "^5.0.0",
    "@types/react": "^18.2.0"
  }
}

脚本运行 #

bash
# 运行脚本
bun run start
bun run dev

# 简写(省略 run)
bun start
bun dev

# 传递参数
bun start --port 3000

# 运行未定义的脚本
bun run build

生命周期脚本 #

json
{
  "scripts": {
    "preinstall": "echo 'Before install'",
    "postinstall": "echo 'After install'",
    "prestart": "echo 'Before start'",
    "start": "bun run src/index.ts",
    "poststart": "echo 'After start'"
  }
}

锁文件 #

bun.lockb #

Bun 使用二进制格式的锁文件 bun.lockb

bash
# 查看锁文件内容
bun pm ls

# 导出为 yarn.lock 格式
bun pm cache rm

锁文件配置 #

toml
# bunfig.toml
[install.lockfile]
save = true
print = "yarn"

配置镜像源 #

使用镜像 #

bash
# 临时使用镜像
bun install --registry https://registry.npmmirror.com

配置文件 #

toml
# bunfig.toml
[install]
registry = "https://registry.npmmirror.com"

常用镜像 #

toml
# 淘宝镜像
registry = "https://registry.npmmirror.com"

# 腾讯镜像
registry = "https://mirrors.cloud.tencent.com/npm/"

# 华为镜像
registry = "https://repo.huaweicloud.com/repository/npm/"

与 npm/yarn/pnpm 对比 #

命令对照表 #

功能 npm yarn pnpm bun
安装依赖 npm install yarn pnpm install bun install
添加依赖 npm add yarn add pnpm add bun add
添加开发依赖 npm add -D yarn add -D pnpm add -D bun add -d
移除依赖 npm rm yarn remove pnpm remove bun remove
更新依赖 npm update yarn upgrade pnpm update bun update
运行脚本 npm run yarn pnpm run bun run
执行包命令 npx yarn dlx pnpm dlx bunx

迁移指南 #

bash
# 从 npm 迁移
rm -rf node_modules package-lock.json
bun install

# 从 yarn 迁移
rm -rf node_modules yarn.lock
bun install

# 从 pnpm 迁移
rm -rf node_modules pnpm-lock.yaml
bun install

常见问题 #

依赖安装失败 #

bash
# 清除缓存
bun pm cache rm

# 删除 node_modules 重新安装
rm -rf node_modules bun.lockb
bun install

私有包配置 #

toml
# bunfig.toml
[install]
# 私有包配置
[install.scopes]
"@mycompany" = { url = "https://npm.mycompany.com", token = "$NPM_TOKEN" }

网络问题 #

bash
# 使用代理
export HTTP_PROXY=http://proxy:8080
export HTTPS_PROXY=http://proxy:8080
bun install

# 或配置
bun install --proxy http://proxy:8080

下一步 #

现在你已经了解了 Bun 包管理的基础知识,接下来学习 工作区管理 了解 Monorepo 支持。

最后更新:2026-03-29