要添加特定的包
bun add preact要指定版本、版本范围或标签
bun add zod@3.20.0bun add zod@^3.0.0bun add zod@latest--dev
别名 — --development, -d, -D
将包添加为开发依赖项("devDependencies")
bun add --dev @types/reactbun add -d @types/react--optional
将包添加为可选依赖项("optionalDependencies")
bun add --optional lodash--peer
将包添加为对等依赖项("peerDependencies")
bun add --peer @types/bun--exact
别名 — -E
要添加一个包并固定到解析后的版本,请使用 --exact。这将解析包的版本并将其添加到您的 package.json 中,使用精确版本号而不是版本范围。
bun add react --exactbun add react -E这将在您的 package.json 中添加以下内容
{
"dependencies": {
// without --exact
"react": "^18.2.0", // this matches >= 18.2.0 < 19.0.0
// with --exact
"react": "18.2.0", // this matches only 18.2.0 exactly
},
}
要查看此命令的完整选项列表
bun add --help--global
注意 — 这不会修改当前项目文件夹的 package.json。 别名 - bun add --global, bun add -g, bun install --global 和 bun install -g
要全局安装一个包,请使用 -g/--global 标志。这不会修改当前项目的 package.json。通常用于安装命令行工具。
bun add --global cowsay # or `bun add -g cowsay`cowsay "Bun!" ______
< Bun! >
------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||配置全局安装行为
受信任的依赖项
与其他 npm 客户端不同,Bun 不会为已安装的依赖项执行任意的生命周期脚本,例如 postinstall。这些脚本存在潜在的安全风险,因为它们可以在您的计算机上执行任意代码。
要告诉 Bun 允许特定包的生命周期脚本,请在您的 package.json 中将该包添加到 trustedDependencies。
{
"name": "my-app",
"version": "1.0.0",
"trustedDependencies": ["my-trusted-package"]
}Bun 会读取此字段,并将为 my-trusted-package 运行生命周期脚本。
Git 依赖项
从公共或私有 git 仓库添加依赖项
bun add git@github.com:moment/moment.git注意 — 要安装私有仓库,您的系统需要有适当的 SSH 凭据来访问该仓库。
Bun 支持多种协议,包括 github、git、git+ssh、git+https 等等。
{
"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 下载并安装该包,而不是从包注册表中获取。
bun add zod@https://registry.npmjs.org/zod/-/zod-3.21.4.tgz这将在您的 package.json 中添加以下行
{
"dependencies": {
"zod": "https://registry.npmjs.org/zod/-/zod-3.21.4.tgz"
}
}