Bun

bun install

bun CLI 包含與 Node.js 相容的套件管理器,旨在成為比 npmyarnpnpm 快上許多的替代品。它是一個獨立工具,可以在現有的 Node.js 專案中運作;如果您的專案有 package.jsonbun install 可以幫助您加速工作流程。

⚡️ 速度快 25 倍 — 在任何 Node.js 專案中從 npm install 切換到 bun install,即可讓您的安裝速度提升高達 25 倍。

對於 Linux 使用者

安裝專案的所有依賴

bun install

執行 bun install 將會

  • 安裝 所有 dependenciesdevDependenciesoptionalDependencies。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"。完整文件請參閱 套件管理器 > 工作區

package.json
{
  "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"。這些是用於指定元相依性(即您的相依套件的相依套件)的版本範圍的機制。完整文件請參閱 套件管理器 > 覆寫和解析

package.json
{
  "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 |
                ||     ||

生產模式

若要在生產模式下安裝(即不含 devDependenciesoptionalDependencies

bun install --production

為了可重現的安裝,請使用 --frozen-lockfile。這將會安裝鎖定檔中指定的每個套件的確切版本。如果您的 package.jsonbun.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 相依套件

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",
    "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

.github/workflows/release.yml
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

CLI 用法

$bun install <name>@<version>

旗標

鎖定檔管理

-y,--yarn
寫入 yarn.lock 檔 (yarn v1)
--no-save
不要更新 package.json 或儲存鎖定檔
--save
儲存至 package.json (預設為 true)
--frozen-lockfile
不允許變更鎖定檔
--save-text-lockfile
儲存基於文字的鎖定檔
--lockfile-only
產生鎖定檔而不安裝相依套件

相依套件管理

-p,--production
不要安裝 devDependencies
-f,--force
永遠從 registry 要求最新版本並重新安裝所有相依套件
--ignore-scripts
略過專案 package.json 中的生命週期腳本 (永遠不會執行相依套件腳本)
--trust
新增至專案 package.json 中的 trustedDependencies 並安裝套件
--omit=<val>
從安裝中排除 'dev'、'optional' 或 'peer' 相依套件
-d,--dev
將相依套件新增至 "devDependencies"
--optional
將相依套件新增至 "optionalDependencies"
--peer
將相依套件新增至 "peerDependencies"
-E,--exact
新增確切版本而不是 ^range
--only-missing
僅在 package.json 中尚未存在時才新增相依套件

安裝行為

--dry-run
不要安裝任何東西
--cache-dir=<val>
從特定目錄路徑儲存和載入快取資料
--no-cache
完全忽略 manifest 快取
--no-verify
略過驗證新下載套件的完整性
-g,--global
全域安裝
--backend=<val>
用於安裝相依套件的平台特定最佳化。可能的值:"clonefile" (預設值)、"hardlink"、"symlink"、"copyfile"
--concurrent-scripts=<val>
生命週期腳本的並行工作最大數量 (預設值 5)
--network-concurrency=<val>
並行網路請求的最大數量 (預設值 48)
-a,--analyze
遞迴分析和安裝作為引數傳遞的檔案的所有相依套件 (使用 Bun 的 bundler)

輸出和記錄

--silent
不要記錄任何內容
--verbose
過於冗長的記錄
--no-progress
停用進度列
--no-summary
不要列印摘要

設定

-c,--config=<val>
指定設定檔路徑 (bunfig.toml)
--ca=<val>
提供憑證授權單位簽署憑證
--cafile=<val>
與 `--ca` 相同,但為憑證的檔案路徑
--cwd=<val>
設定特定的 cwd
--registry=<val>
預設使用特定的 registry,覆寫 .npmrc、bunfig.toml 和環境變數
--filter=<val>
為符合的工作區安裝套件

說明

-h,--help
列印此說明選單

範例

為當前專案安裝相依套件
bun install
略過 devDependencies
bun install --production
完整文件請見 https://bun.dev.org.tw/docs/cli/install