Bun

bun add

新增特定套件

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 --globalbun 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 支援多種協定,包括 githubgitgit+sshgit+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

package.json
{
  "dependencies": {
    "zod": "https://registry.npmjs.org/zod/-/zod-3.21.4.tgz"
  }
}

CLI 用法

$bun add <package> <@version>

旗標

Lockfile 選項

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

相依性類型

-p,--production
不要安裝 devDependencies
-d,--dev
將相依性新增到 "devDependencies"
--optional
將相依性新增到 "optionalDependencies"
--peer
將相依性新增到 "peerDependencies"

安全性與信任

--ca=<val>
提供憑證授權單位簽署憑證
--cafile=<val>
與 `--ca` 相同,但這是憑證的檔案路徑
--no-verify
略過驗證新下載套件的完整性
--trust
新增到專案 package.json 中的 trustedDependencies 並安裝套件

安裝行為

--dry-run
不要安裝任何東西
-f,--force
總是從登錄檔請求最新版本 & 重新安裝所有相依性
--ignore-scripts
略過專案 package.json 中的生命週期腳本 (永遠不會執行相依性腳本)
--only-missing
僅在 package.json 中尚未存在相依性時才新增它們

快取與效能

--cache-dir=<val>
從特定目錄路徑儲存 & 載入快取資料
--no-cache
完全忽略 manifest 快取
--backend=<val>
安裝相依性的平台特定最佳化。可能的值:「clonefile」(預設值)、「hardlink」、「symlink」、「copyfile」
--concurrent-scripts=<val>
生命週期腳本的最大並行工作數(預設值為 5)
--network-concurrency=<val>
最大並行網路請求數(預設值為 48)

記錄與輸出

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

設定

-c,--config=<val>
指定設定檔路徑 (bunfig.toml)
--registry=<val>
預設使用特定登錄檔,覆寫 .npmrc、bunfig.toml 和環境變數
--omit=<val>
從安裝中排除 'dev'、'optional' 或 'peer' 相依性

工作區

-g,--global
全域安裝
--cwd=<val>
設定特定的 cwd

其他

-E,--exact
新增精確版本而非 ^range
-a,--analyze
遞迴分析 & 安裝作為引數傳遞的檔案的相依性 (使用 Bun 的 bundler)
-h,--help
列印此說明選單

範例

從 npm 登錄檔新增相依性
bun add zod
bun add zod@next
新增 dev、optional 或 peer 相依性
bun add -d typescript
bun add --optional lodash
完整文件請參閱 https://bun.dev.org.tw/docs/cli/add