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

--exact

若要新增套件並固定到已解析的版本,請使用 --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 --globalbun add -gbun 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

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