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.lockb 鎖定檔至專案根目錄。

記錄

若要修改記錄詳細資料

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

覆寫和解析

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。這將安裝 lockfile 中指定的每個套件的確切版本。如果您的 package.jsonbun.lockb 不一致,Bun 會產生錯誤並結束。lockfile 也不會更新。

bun install --frozen-lockfile

有關 Bun 的二進制 lockfile bun.lockb 的更多資訊,請參閱 套件管理員 > Lockfile

執行測試

若要執行測試(即實際上不安裝任何內容)

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 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 `--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@v1
      - name: Install dependencies
        run: bun install
      - name: Build app
        run: bun run build