bun
CLI 包含與 Node.js 相容的套件管理器,旨在成為比 npm
、yarn
和 pnpm
快上許多的替代品。它是一個獨立工具,可以在現有的 Node.js 專案中運作;如果您的專案有 package.json
,bun install
可以幫助您加速工作流程。
對於 Linux 使用者
安裝專案的所有依賴
bun install
執行 bun install
將會
- 安裝 所有
dependencies
、devDependencies
和optionalDependencies
。Bun 預設會安裝peerDependencies
。 - 在適當的時機執行專案的
{pre|post}install
和{pre|post}prepare
腳本。基於安全考量,Bun不會執行已安裝相依套件的生命週期腳本。 - 寫入
bun.lock
鎖定檔至專案根目錄。
記錄
若要修改記錄詳細程度
bun install --verbose # debug logging
bun install --silent # no logging
生命週期腳本
與其他 npm 客戶端不同,Bun 不會為已安裝的相依套件執行任意生命週期腳本,例如 postinstall
。執行任意腳本代表潛在的安全風險。
若要告知 Bun 允許特定套件的生命週期腳本,請將該套件新增至 package.json 中的 trustedDependencies
。
{
"name": "my-app",
"version": "1.0.0",
"trustedDependencies": ["my-trusted-package"]
}
然後重新安裝該套件。Bun 將會讀取此欄位並為 my-trusted-package
執行生命週期腳本。
生命週期腳本將在安裝期間並行執行。若要調整並行腳本的最大數量,請使用 --concurrent-scripts
旗標。預設值為回報的 CPU 核心數或 GOMAXPROCS 的兩倍。
bun install --concurrent-scripts 5
工作區
Bun 支援 package.json 中的 "workspaces"
。完整文件請參閱 套件管理器 > 工作區。
{
"name": "my-app",
"version": "1.0.0",
"workspaces": ["packages/*"],
"dependencies": {
"preact": "^10.5.13"
}
}
為特定套件安裝相依套件
在 monorepo 中,您可以使用 --filter
旗標為套件的子集安裝相依套件。
# Install dependencies for all workspaces except `pkg-c`
bun install --filter '!pkg-c'
# Install dependencies for only `pkg-a` in `./packages/pkg-a`
bun install --filter './packages/pkg-a'
有關使用 bun install
進行篩選的更多資訊,請參閱 套件管理器 > 篩選
覆寫和解析
Bun 支援 package.json 中的 npm 的 "overrides"
和 Yarn 的 "resolutions"
。這些是用於指定元相依性(即您的相依套件的相依套件)的版本範圍的機制。完整文件請參閱 套件管理器 > 覆寫和解析。
{
"name": "my-app",
"dependencies": {
"foo": "^2.0.0"
},
"overrides": {
"bar": "~4.4.0"
}
}
全域套件
若要全域安裝套件,請使用 -g
/--global
旗標。通常這用於安裝命令列工具。
bun install --global cowsay # or `bun install -g cowsay`
cowsay "Bun!"
______
< Bun! >
------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
生產模式
若要在生產模式下安裝(即不含 devDependencies
或 optionalDependencies
)
bun install --production
為了可重現的安裝,請使用 --frozen-lockfile
。這將會安裝鎖定檔中指定的每個套件的確切版本。如果您的 package.json
與 bun.lock
不一致,Bun 將會以錯誤退出。鎖定檔將不會被更新。
bun install --frozen-lockfile
有關 Bun 的鎖定檔 bun.lock
的更多資訊,請參閱 套件管理器 > 鎖定檔。
省略相依套件
若要省略 dev、peer 或 optional 相依套件,請使用 --omit
旗標。
# Exclude "devDependencies" from the installation. This will apply to the
# root package and workspaces if they exist. Transitive dependencies will
# not have "devDependencies".
bun install --omit dev
# Install only dependencies from "dependencies"
bun install --omit=dev --omit=peer --omit=optional
試執行
若要執行試執行(即實際上不安裝任何東西)
bun install --dry-run
非 npm 相依套件
Bun 支援從 Git、GitHub 和本機或遠端託管的 tarball 安裝相依套件。完整文件請參閱 套件管理器 > Git、GitHub 和 tarball 相依套件。
{
"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",
"react": "https://registry.npmjs.org/react/-/react-18.2.0.tgz",
"bun-types": "npm:@types/bun"
}
}
設定
bun install
的預設行為可以在 bunfig.toml
中設定。預設值如下所示。
[install]
# whether to install optionalDependencies
optional = true
# whether to install devDependencies
dev = true
# whether to install peerDependencies
peer = true
# equivalent to `--production` flag
production = false
# equivalent to `--save-text-lockfile` flag
saveTextLockfile = false
# equivalent to `--frozen-lockfile` flag
frozenLockfile = false
# equivalent to `--dry-run` flag
dryRun = false
# equivalent to `--concurrent-scripts` flag
concurrentScripts = 16 # (cpu count or GOMAXPROCS) x2
CI/CD
想要加速您的 CI 嗎?使用官方的 oven-sh/setup-bun
動作在 GitHub Actions 管道中安裝 bun
。
name: bun-types
jobs:
build:
name: build-app
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: Install bun
uses: oven-sh/setup-bun@v2
- name: Install dependencies
run: bun install
- name: Build app
run: bun run build