> ## Documentation Index
> Fetch the complete documentation index at: https://bun.ll1025.cn/llms.txt
> Use this file to discover all available pages before exploring further.

# .npmrc 支持

Bun 从 [`.npmrc`](https://docs.npmjs.com/cli/v10/configuring-npm/npmrc) 文件加载配置选项，因此你可以重用现有的注册表和作用域配置。

<Note>
  我们建议将你的 `.npmrc` 文件迁移为 Bun 的 [`bunfig.toml`](/runtime/bunfig) 格式，它支持更多选项，包括 Bun 特定的选项。
</Note>

***

## 支持的选项

### 设置默认注册表

Bun 从默认注册表解析包，即 `npm` 的官方注册表（`https://registry.npmjs.org/`）。

要更改它，请在 `.npmrc` 中设置 `registry` 选项：

```ini .npmrc icon="npm" theme={null}
registry=http://localhost:4873/
```

等效的 `bunfig.toml` 选项是 [`install.registry`](/runtime/bunfig#install-registry)：

```toml bunfig.toml icon="settings" theme={null}
install.registry = "http://localhost:4873/"
```

### 为特定作用域设置注册表

`@<scope>:registry` 为特定作用域设置注册表：

```ini .npmrc icon="npm" theme={null}
@myorg:registry=http://localhost:4873/
```

等效的 `bunfig.toml` 选项是在 [`install.scopes`](/runtime/bunfig#install-registry) 中添加一个键：

```toml bunfig.toml icon="settings" theme={null}
[install.scopes]
myorg = "http://localhost:4873/"
```

### 为特定注册表配置选项

`//<registry_url>/:<key>=<value>` 为特定注册表设置选项：

```ini .npmrc icon="npm" theme={null}
# 为注册表设置认证令牌
# ${...} 是环境变量的占位符
//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 编码的 username:password，例如 `btoa(username + ":" + password)`）
* `email`

等效的 `bunfig.toml` 选项是在 [`install.scopes`](/runtime/bunfig#install-registry) 中添加一个键：

```toml bunfig.toml icon="settings" theme={null}
[install.scopes]
myorg = { url = "http://localhost:4873/", username = "myusername", password = "$NPM_PASSWORD" }
```

### `link-workspace-packages`：控制工作空间包安装

控制当本地可用时如何安装工作空间包：

```ini .npmrc icon="npm" theme={null}
link-workspace-packages=true
```

等效的 `bunfig.toml` 选项是 [`install.linkWorkspacePackages`](/runtime/bunfig#install-linkworkspacepackages)：

```toml bunfig.toml icon="settings" theme={null}
[install]
linkWorkspacePackages = true
```

### `save-exact`：保存精确版本

始终保存精确版本，不添加 `^` 前缀：

```ini .npmrc icon="npm" theme={null}
save-exact=true
```

等效的 `bunfig.toml` 选项是 [`install.exact`](/runtime/bunfig#install-exact)：

```toml bunfig.toml icon="settings" theme={null}
[install]
exact = true
```

### `ignore-scripts`：跳过生命周期脚本

阻止在安装期间运行生命周期脚本：

```ini .npmrc icon="npm" theme={null}
ignore-scripts=true
```

这等效于在 `bun install` 中使用 `--ignore-scripts` 标志。

### `dry-run`：预览更改而不安装

显示将要安装的内容而不实际安装任何东西：

```ini .npmrc icon="npm" theme={null}
dry-run=true
```

等效的 `bunfig.toml` 选项是 [`install.dryRun`](/runtime/bunfig#install-dryrun)：

```toml bunfig.toml icon="settings" theme={null}
[install]
dryRun = true
```

### `cache`：配置缓存目录

设置缓存目录路径，或禁用缓存：

```ini .npmrc icon="npm" theme={null}
# 设置自定义缓存目录
cache=/path/to/cache

# 或禁用缓存
cache=false
```

等效的 `bunfig.toml` 选项是 [`install.cache`](/runtime/bunfig#install-cache)：

```toml bunfig.toml icon="settings" theme={null}
[install.cache]
# 设置自定义缓存目录
dir = "/path/to/cache"

# 或禁用缓存
disable = true
```

### `ca` 和 `cafile`：配置 CA 证书

为注册表连接配置自定义 CA 证书：

```ini .npmrc icon="npm" theme={null}
# 单个 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`：控制依赖类型

控制安装哪些依赖类型：

```ini .npmrc icon="npm" theme={null}
# 省略开发依赖
omit=dev

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

# 包含特定类型（覆盖 omit）
include=dev
```

有效值：`dev`、`peer`、`optional`

### `install-strategy` 和 `node-linker`：安装策略

控制包在 `node_modules` 中的布局方式。为了与其他包管理器兼容，Bun 同时接受 npm 的 `install-strategy` 和 pnpm/yarn 的 `node-linker`。请参阅[隔离安装](/pm/isolated-installs)了解提升布局和隔离布局的区别。

**npm 的 `install-strategy`：**

```ini .npmrc icon="npm" theme={null}
# 扁平的 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 |

```ini .npmrc icon="npm" theme={null}
# 符号链接/隔离模式
node-linker=isolated
node-linker=pnpm

# 扁平/提升模式
node-linker=hoisted
node-linker=node-modules
```

### `public-hoist-pattern` 和 `hoist-pattern`：控制提升

控制哪些包被提升到根 `node_modules`：

```ini .npmrc icon="npm" theme={null}
# 匹配此模式的包将被提升到根目录
public-hoist-pattern=*eslint*

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

# 控制通用的提升行为
hoist-pattern=*
```
