添加特定包
bun add preact
指定版本、版本范围或标签
bun add zod@3.20.0
bun add zod@^3.0.0
bun add zod@latest
--dev
别名 — --development
, -d
, -D
将包添加为开发依赖项 ("devDependencies"
)
bun add --dev @types/react
bun 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 --exact
bun 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"
}
}