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

# 使用 bun install 配合 Azure Artifacts npm 注册表

<Note>
  在 [Azure
  Artifacts](https://learn.microsoft.com/en-us/azure/devops/artifacts/npm/npmrc?view=azure-devops\&tabs=windows%2Cclassic)
  关于 `.npmrc` 的说明中，建议对密码进行 base64 编码。但在使用 `bun install` 时不要这样做。Bun 会在需要时自动为你进行 base64 编码。
</Note>

[Azure Artifacts](https://azure.microsoft.com/en-us/products/devops/artifacts) 是 Azure DevOps 提供的包管理系统。它允许你托管私有的 npm 注册表、npm 包以及其他类型的包。

***

### 使用 bunfig.toml 配置

***

要配合 `bun install` 使用，在项目中添加一个名为 `bunfig.toml` 的文件，内容如下。确保将 `my-azure-artifacts-user` 替换为你的 Azure Artifacts 用户名，例如 `jarred1234`。

```toml bunfig.toml icon="settings" theme={"theme":{"light":"github-light","dark":"dracula"}}
[install.registry]
url = "https://pkgs.dev.azure.com/my-azure-artifacts-user/_packaging/my-azure-artifacts-user/npm/registry"
username = "my-azure-artifacts-user"
# 这里可以使用环境变量
password = "$NPM_PASSWORD"
```

***

然后，将你的 Azure 个人访问令牌赋值给环境变量 `NPM_PASSWORD`。Bun 会[自动读取](/runtime/environment-variables) `.env` 文件，因此在项目根目录创建一个名为 `.env` 的文件即可。不需要对令牌进行 base64 编码，Bun 会帮你处理。

```ini .env icon="settings" theme={"theme":{"light":"github-light","dark":"dracula"}}
NPM_PASSWORD=<在此粘贴令牌>
```

***

### 使用环境变量配置

***

如果不使用 `bunfig.toml`，你可以设置环境变量 `NPM_CONFIG_REGISTRY` 来配置 Azure Artifacts。URL 中应包含 `:username` 和 `:_password` 作为查询参数，替换 `<USERNAME>` 和 `<PASSWORD>` 为相应的值。

```bash terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
NPM_CONFIG_REGISTRY=https://pkgs.dev.azure.com/my-azure-artifacts-user/_packaging/my-azure-artifacts-user/npm/registry/:username=<USERNAME>:_password=<PASSWORD>
```

***

### 不要对密码进行 base64 编码

***

在 [Azure Artifacts](https://learn.microsoft.com/en-us/azure/devops/artifacts/npm/npmrc?view=azure-devops\&tabs=windows%2Cclassic) 的 `.npmrc` 说明中会建议对密码进行 base64 编码，但使用 `bun install` 时切勿这样做。Bun 会自动为你进行 base64 编码（如果需要的话）。

<Note>**提示** — 如果密码以 `==` 结尾，很可能就是 base64 编码的。</Note>

***

要解码 base64 编码的密码，可以打开浏览器控制台执行：

```js browser icon="computer" theme={"theme":{"light":"github-light","dark":"dracula"}}
atob("<base64编码的密码>");
```

***

或者使用命令行工具 `base64`，但这样可能会被保存到终端历史记录中，出于安全考虑不建议这么做：

```bash terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
echo "base64编码的密码" | base64 --decode
```
