Skip to main content
添加特定包:
terminal
bun add preact
指定版本、版本范围或标签:
terminal
bun add [email protected]
bun add zod@^3.0.0
bun add zod@latest

--dev

别名--development-d-D
将包作为开发依赖("devDependencies")添加:
terminal
bun add --dev @types/react
bun add -d @types/react

--optional

将包作为可选依赖("optionalDependencies")添加:
terminal
bun add --optional lodash

--peer

将包作为对等依赖("peerDependencies")添加:
terminal
bun add --peer @types/bun

--exact

别名-E
使用 --exact 添加包并固定解析的版本。此操作将解析包的版本并以精确的版本号添加到您的 package.json 中,而非版本范围。
terminal
bun add react --exact
bun add react -E
这将在您的 package.json 中添加如下内容:
package.json
{
  "dependencies": {
    // 不使用 --exact
    "react": "^18.2.0", // 匹配 >= 18.2.0 < 19.0.0

    // 使用 --exact
    "react": "18.2.0" // 仅精确匹配 18.2.0 版本
  }
}
查看此命令的完整选项列表:
terminal
bun add --help

--global

注意 — 这不会修改当前项目文件夹的 package.json。别名 - bun add --globalbun add -gbun install --global 以及 bun install -g
使用 -g/--global 标志全局安装包。这不会修改您当前项目的 package.json。通常用于安装命令行工具。
terminal
bun add --global cowsay # 或 `bun add -g cowsay`
cowsay "Bun!"
 ______
< Bun! >
 ------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||
bunfig.toml
[install]
# `bun add --global` 安装包的位置
globalDir = "~/.bun/install/global"

# 全局安装的包二进制文件链接位置
globalBinDir = "~/.bun/bin"

受信任的依赖

与其它 npm 客户端不同,Bun 不会执行已安装依赖的任意生命周期脚本,如 postinstall。这些脚本存在安全风险,可能在您的机器上执行任意代码。 要让 Bun 允许特定包的生命周期脚本,请在您的 package.json 中将该包添加到 trustedDependencies
package.json
{
  "name": "my-app",
  "version": "1.0.0",
  "trustedDependencies": ["my-trusted-package"] 
}
Bun 读取此字段,并允许 my-trusted-package 执行生命周期脚本。

Git 依赖

添加来自公共或私有 Git 仓库的依赖:
terminal
bun add [email protected]:moment/moment.git
安装私有仓库时,系统需具备相应的 SSH 凭据以访问该仓库。
Bun 支持多种协议,包括 githubgitgit+sshgit+https 等。
package.json
{
  "dependencies": {
    "dayjs": "git+https://github.com/iamkun/dayjs.git",
    "lodash": "git+ssh://github.com/lodash/lodash.git#4.17.21",
    "moment": "[email protected]:moment/moment.git",
    "zod": "github:colinhacks/zod"
  }
}

Tarball 依赖

包名可以对应公开托管的 .tgz 文件。安装时,Bun 将从指定的 tarball URL 下载并安装包,而非从包注册表。
terminal
bun add zod@https://registry.npmjs.org/zod/-/zod-3.21.4.tgz
这将在您的 package.json 中添加如下行:
package.json
{
  "dependencies": {
    "zod": "https://registry.npmjs.org/zod/-/zod-3.21.4.tgz"
  }
}

CLI 使用方法

bun add <package> <@version>

依赖管理

--production
boolean
不安装 devDependencies。别名:-p
--omit
string
安装时排除 devoptionalpeer 依赖
--global
boolean
全局安装。别名:-g
--dev
boolean
添加依赖到 devDependencies。别名:-d
--optional
boolean
添加依赖到 optionalDependencies
--peer
boolean
添加依赖到 peerDependencies
--exact
boolean
添加精确版本,而非 ^ 范围。别名:-E
--only-missing
boolean
仅在 package.json 中不存在该依赖时才添加

项目文件与锁文件

--yarn
boolean
生成 yarn.lock 文件(yarn v1)。别名:-y
--no-save
boolean
不更新 package.json 或保存锁文件
--save
boolean
default:"true"
保存至 package.json(默认开启)
--frozen-lockfile
boolean
不允许修改锁文件
--trust
boolean
将依赖添加到项目 package.jsontrustedDependencies 并安装该包
--save-text-lockfile
boolean
保存为文本格式的锁文件
--lockfile-only
boolean
仅生成锁文件,不安装依赖

安装控制

--dry-run
boolean
不执行实际安装
--force
boolean
总是从注册表请求最新版本并重新安装所有依赖。别名:-f
--no-verify
boolean
跳过新下载包的完整性校验
--ignore-scripts
boolean
跳过项目 package.json 中的生命周期脚本(依赖的脚本永远不会执行)
--analyze
boolean
递归分析并安装作为参数传入文件的依赖(使用 Bun 的打包器)。别名:-a

网络与注册表

--ca
string
提供证书颁发机构签名证书
--cafile
string
—ca 相同,但通过证书文件路径提供
--registry
string
默认使用指定注册表,覆盖 .npmrcbunfig.toml 和环境变量
--network-concurrency
number
default:"48"
最大并发网络请求数(默认 48)

性能与资源

--backend
string
default:"clonefile"
安装依赖的特定平台优化。可选值:clonefile(默认)、hardlinksymlinkcopyfile
--concurrent-scripts
number
default:"5"
生命周期脚本的最大并发任务数(默认 5)

缓存

--cache-dir
string
从指定目录路径存储和加载缓存数据
--no-cache
boolean
完全忽略清单缓存

输出与日志

--silent
boolean
不输出任何日志
--verbose
boolean
输出超详细日志
--no-progress
boolean
关闭进度条
--no-summary
boolean
不打印总结信息

全局配置与上下文

--config
string
指定配置文件路径(bunfig.toml)。别名:-c
--cwd
string
设置当前工作目录

帮助

--help
boolean
显示此帮助菜单。别名:-h