Skip to main content
要添加特定包:
terminal
bun add preact
要指定版本、版本范围或标签:
terminal
bun add zod@3.20.0
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 --global, bun add -g, bun install --globalbun 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 git@github.com:moment/moment.git
要安装私有仓库,你的系统需要适当的 SSH 凭据来访问仓库。
Bun 支持多种协议,包括 github, git, git+ssh, git+https 等等。
package.json
{
  "dependencies": {
    "dayjs": "git+https://github.com/iamkun/dayjs.git",
    "lodash": "git+ssh://github.com/lodash/lodash.git#4.17.21",
    "moment": "git@github.com: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(默认为 true)
--frozen-lockfile
boolean
禁止更改锁文件
--trust
boolean
添加到项目 package.json 中的 trustedDependencies 并安装包
--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