執行環境套件管理器打包器測試執行器程式碼檢查格式化指南參考部落格意見回饋

.npmrc 支援

Bun 會從 .npmrc 檔案中載入設定選項,因此你可以複用現有的登錄檔和作用域設定。

設定按以下順序載入,後面的來源會覆蓋前面的來源:

  1. ~/.npmrc(或 $XDG_CONFIG_HOME/.npmrc)
  2. ./.npmrc
  3. bunfig.toml(全域性,然後專案)
  4. BUN_CONFIG_REGISTRY / NPM_CONFIG_REGISTRY 和 BUN_CONFIG_TOKEN / NPM_CONFIG_TOKEN 環境變數
  5. --registry 等命令列標誌

Bun 會將 .npmrc 中的憑據(//<registry>/:_authToken 等)按主機和路徑匹配到登錄檔,即使你在 bunfig.toml 中設定了登錄檔 URL 也是如此。

值可以引用環境變數。Bun 會將 ${NAME} 替換為該變數的值;如果變數未設定,則保留原樣。如果變數未設定,${NAME?} 會變為空字串。


支援的選項

設定預設的 registry

Bun 從預設 registry 中解析軟體包,該 registry 是 npm 的官方 registry(https://registry.npmjs.org/)。

要更改它,請在 .npmrc 中設定 registry 選項:

.npmrc
registry=http://localhost:4873/

對應的 bunfig.toml 選項是 install.registry:

bunfig.toml
install.registry = "http://localhost:4873/"

為特定作用域設定 registry

@<scope>:registry 為特定作用域設定 registry:

.npmrc
@myorg:registry=http://localhost:4873/

對應的 bunfig.toml 選項是在 install.scopes 中新增一個鍵:

bunfig.toml
[install.scopes]
myorg = "http://localhost:4873/"

設定特定 registry 的選項

//<registry_url>/:<key>=<value> 為特定 registry 設定選項:

.npmrc
# 為 registry 設定認證令牌
# ${...} 是環境變數的佔位符
//http://localhost:4873/:_authToken=${NPM_TOKEN}


# 或者你可以設定使用者名稱和密碼
# 注意密碼是 base64 編碼過的
//http://localhost:4873/:username=myusername

//http://localhost:4873/:_password=${NPM_PASSWORD}

# 或者使用 _auth,它是使用者名稱和密碼合併成一個字串後 base64 編碼的結果
//http://localhost:4873/:_auth=${NPM_AUTH}

Bun 支援以下選項:

  • _authToken
  • username
  • _password(經過 base64 編碼的密碼)
  • _auth(經過 base64 編碼的 username:password,例如 btoa(username + ":" + password))
  • email

對應的 bunfig.toml 選項是在 install.scopes 中新增一個鍵:

bunfig.toml
[install.scopes]
# 與 .npmrc 中的 _password 不同,password 未經過 base64 編碼;Bun 會為你編碼
myorg = { url = "http://localhost:4873/", username = "myusername", password = "$NPM_PASSWORD" }

控制 Bun 在工作區包可在本地獲取時如何安裝它們:

.npmrc
link-workspace-packages=true

對應的 bunfig.toml 選項是 install.linkWorkspacePackages:

bunfig.toml
[install]
linkWorkspacePackages = true

save-exact:儲存確切版本

始終儲存確切版本,不帶 ^ 字首:

.npmrc
save-exact=true

對應的 bunfig.toml 選項是 install.exact:

bunfig.toml
[install]
exact = true

ignore-scripts:跳過生命週期指令碼

防止在安裝時執行生命週期指令碼:

.npmrc
ignore-scripts=true

這相當於使用 bun install 的 --ignore-scripts 標誌。

dry-run:預覽變更但不安裝

顯示 Bun 在不實際安裝任何內容的情況下會安裝什麼:

.npmrc
dry-run=true

對應的 bunfig.toml 選項是 install.dryRun:

bunfig.toml
[install]
dryRun = true

cache:設定快取目錄

設定快取目錄路徑,或停用快取:

.npmrc
# 設定自定義快取目錄
cache=/path/to/cache

# 或停用快取
cache=false

對應的 bunfig.toml 選項是 install.cache:

bunfig.toml
[install.cache]
# 設定自定義快取目錄
dir = "/path/to/cache"

# 或停用快取
disable = true

ca 和 cafile:設定 CA 證書

為 registry 連線設定自定義 CA 證書:

.npmrc
# 單個 CA 證書
ca="-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----"

# 多個 CA 證書
ca[]="-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----"
ca[]="-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----"

# 或指定 CA 檔案路徑
cafile=/path/to/ca-bundle.crt

omit 和 include:控制相依型別

控制 Bun 安裝哪些型別的相依:

.npmrc
# 忽略開發相依
omit=dev

# 忽略多種型別
omit[]=dev
omit[]=optional

# 包含指定型別(覆蓋 omit)
include=dev

有效值:dev,peer,optional

install-strategy 和 node-linker:安裝策略

控制 Bun 在 node_modules 中如何組織軟體包。為相容其他套件管理器,Bun 同時接受 npm 的 install-strategy 和 pnpm/yarn 的 node-linker。有關托舉式和隔離式佈局的區別,請參閱隔離安裝。

npm 的 install-strategy:

.npmrc
# 扁平 node_modules 結構(預設)
install-strategy=hoisted

# 符號連結結構
install-strategy=linked

pnpm / yarn 的 node-linker:

node-linker 控制安裝模式。Bun 接受來自 pnpm 和 yarn 的值:

值描述接受者
isolated符號連結結構,帶有隔離相依pnpm
hoisted扁平 node_modules 結構pnpm
pnpm符號連結結構(等同於 isolated)yarn
node-modules扁平 node_modules 結構(等同於 hoisted)yarn
.npmrc
# 符號連結 / 隔離模式
node-linker=isolated
node-linker=pnpm

# 扁平 / 托舉模式
node-linker=hoisted
node-linker=node-modules

public-hoist-pattern 和 hoist-pattern:控制托舉

控制 Bun 將哪些軟體包托舉到根目錄的 node_modules:

.npmrc
# 匹配該模式的包將被托舉到根目錄
public-hoist-pattern=*eslint*

# 多個模式
public-hoist-pattern[]=*eslint*
public-hoist-pattern[]=*prettier*

# 控制通用的托舉行為
hoist-pattern=*

# 僅適用於 isolated linker:停用 node_modules/.bun/node_modules 回退機制,
# 使儲存包只解析已宣告的相依項(以及根目錄的 node_modules)。
# 托舉式安裝會忽略此設定。
hoist=false