Skip to main content
Bun 支持从 .npmrc 文件加载配置选项,允许你重用现有的 registry/作用域配置。
我们建议将你的 .npmrc 文件迁移到 Bun 的 bunfig.toml 格式,因为它提供了更灵活的选项,并且可以让你配置 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}
支持的选项包括:
  • _authToken
  • username
  • _password(base64 编码的密码)
  • _auth(base64 编码的用户名:密码,例如 btoa(username + ":" + password)
  • email
对应的 bunfig.toml 选项是在 install.scopes 中添加一个键:
bunfig.toml
[install.scopes]
myorg = { url = "http://localhost:4873/", username = "myusername", password = "$NPM_PASSWORD" }
控制本地可用时工作区包的安装方式:
.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: 预览变更但不安装

显示将会安装的内容,但实际不进行安装:
.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

cacafile: 配置 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

omitinclude: 控制依赖类型

控制安装哪些依赖类型:
.npmrc
# 忽略开发依赖
omit=dev

# 忽略多种类型
omit[]=dev
omit[]=optional

# 包含指定类型(覆盖 omit)
include=dev
有效值:devpeeroptional

install-strategynode-linker: 安装策略

控制 node_modules 中包的安装方式。Bun 支持两种不同的配置选项,以兼容不同的包管理器。 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符号链接结构(等同于 isolatedyarn
node-modules扁平 node_modules 结构(等同于 hoistedyarn
.npmrc
# 符号链接 / 隔离模式
node-linker=isolated
node-linker=pnpm

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

public-hoist-patternhoist-pattern: 控制托举

控制哪些包被托举到根节点的 node_modules
.npmrc
# 匹配该模式的包将被托举到根目录
public-hoist-pattern=*eslint*

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

# 控制通用的托举行为
hoist-pattern=*