跳到主内容
版本:10.x

身份验证 & 注册源设置 (.npmrc)

此页面上的设置必须通过 .npmrc 文件进行配置。 pnpm 底层使用 npm CLI 进行发布,因此这些设置需要采用 npm 可以读取的格式。

有关如何加载 .npmrc 文件的详细信息,请参阅 pnpm config 命令。

对于可以在 pnpm-workspace.yaml 中配置的设置,请参阅 设置 (pnpm-workspace.yaml)

环境变量

.npmrc 文件中的值可以使用 ${NAME} 语法引用环境变量。

自 v10.34.2 版本起,对于以下设置,项目和工作区 .npmrc 文件(即随仓库一同检出的文件)中的环境变量将不再进行展开:

  • 注册源和代理 URL(registry@scope:registryproxyhttps-proxyhttp-proxy);
  • URL-scoped keys (keys starting with //);
  • credential values (_authToken, _auth, _password, username, tokenHelper, cert, key).

A setting that contains a ${...} placeholder in any of these positions is ignored, and pnpm prints a warning. Repository-controlled .npmrc files must not be able to expand environment variables into the URLs pnpm sends requests to, or into the credentials attached to those requests — otherwise a malicious repository could exfiltrate secrets from your environment (such as CI tokens) to an attacker-controlled registry during installation (GHSA-3qhv-2rgh-x77r).

If your project relied on a committed .npmrc containing a line like //registry.npmjs.org/:_authToken=${NPM_TOKEN}, move the token to a trusted location instead:

  • Write the token to the global config file before installing (for example, in a CI step):

    pnpm config set //registry.npmjs.org/:_authToken "$NPM_TOKEN"

    pnpm config set writes to the global config file by default, not to the project .npmrc, so the token never ends up in the repository.

  • Or keep the ${NPM_TOKEN} placeholder line, but put it in the user-level ~/.npmrc instead of the repository — environment variables are still expanded there.

  • Or set the value via an environment variable directly, without any .npmrc entry:

    npm_config_//registry.npmjs.org/:_authToken=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
  • In GitHub Actions, actions/setup-node with the registry-url input writes the auth setting to a user-level .npmrc (referenced by the NPM_CONFIG_USERCONFIG environment variable, which pnpm honors), so authentication via the NODE_AUTH_TOKEN environment variable continues to work.

  • If you cannot easily modify each CI pipeline, you may declare the project .npmrc trusted by setting a single environment variable in the CI environment (for example, at the organization or workspace level):

    NPM_CONFIG_USERCONFIG=.npmrc

    This makes pnpm read the project's .npmrc as the user-level config file (a relative path is resolved against the working directory), so environment variables in it are expanded as before. 由于信任声明来自环境而非仓库,恶意仓库无法代你进行此设置。

    警告

    仅在专门构建受信任仓库的环境中使用此设置。 It disables this protection entirely for the checked-out repository, including the restriction that tokenHelper may only be set in user-level config.

The same rule applies to registry and proxy URLs in a project or workspace .npmrc (registry, @scope:registry, proxy, https-proxy, http-proxy). If you used an environment variable to build a registry URL, move the setting to a trusted source — your user-level ~/.npmrc, the global config (pnpm config set "<key>" <value>), a CLI option, or an environment variable. If the URL is not secret, you can also write the resolved value directly in the project .npmrc, since only ${...} placeholders are ignored.

注册源设置

注册源

npm 包注册源地址 (包括末尾斜杠) 。

@jsr:registry

添加于:v10.9.0

JSR 包注册源的基础网址。

<scope>:registry

用于指定包的注册源范围 例如,设置 @babel:registry=https://example.com/packages/npm/ 将在你使用 pnpm add @babel/core 或任何 @babel 范围的包时,该包将强制从 https://example.com/packages/npm 而不是默认注册源中获取。

认证设置

<URL>:_authToken

访问指定注册源时要使用的身份验证承载令牌。 例如:

//registry.npmjs.org/:_authToken=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

你还可以使用环境变量。 例如:

//registry.npmjs.org/:_authToken=${NPM_TOKEN}

Since v10.34.2, environment variables are only expanded in trusted locations (the user ~/.npmrc, the global config, CLI options, and npm_config_* environment variables), not in the project or workspace .npmrc. See Environment variables.

或者,你可以在完全不更改 .npmrc 下直接使用环境变量:

npm_config_//registry.npmjs.org/:_authToken=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

<URL>:tokenHelper

令牌助手是输出身份验证令牌的可执行文件。 这可以用于 authToken 不是常量值而是定期刷新值的情况,其中脚本或其他工具可以使用现有的刷新令牌来获取新的访问令牌。

助手路径的配置必须是绝对路径,没有参数。 为了安全起见,只允许在用户 .npmrc 中设置该值。 否则,项目可以在项目的本地 .npmrc 中放置一个值并运行任意可执行文件。

为默认注册表设置令牌助手:

tokenHelper=/home/ivan/token-generator

为指定注册源设置令牌助手:

//registry.corp.com:tokenHelper=/home/ivan/token-generator

代理设置

https-proxy

  • 默认值: null
  • 类型:url

用于传出 HTTPS 请求的代理。 如果设置了 HTTPS_PROXYhttps_proxyHTTP_PROXYhttp_proxy 环境变量,将使用环境变量的值。

如果你的代理 URL 包含用户名和密码,请确保对它们进行 URL 编码。 例如:

https-proxy=https://use%21r:pas%2As@my.proxy:1234/foo

不要对用户名和密码之间的冒号 (:) 进行编码。

http-proxy

proxy

  • 默认值: null
  • 类型:url

用于传出 HTTP 请求的代理。 如果设置了 HTTP_PROXY 或 http_proxy 环境变量,底层请求库将遵循代理设置。

local-address

  • 默认值:undefined
  • 类型:IP 地址

与 npm 注册源建立连接时要使用的本地接口的 IP 地址。

maxsockets

  • 默认值:网络并发 x 3
  • 类型:Number

每个源(由协议/主机/端口号组合而成)允许的最大连接数。

noproxy

  • 默认值: null
  • 类型:字符串

一个由逗号分割的域名字符串,表示不应该被使用的代理

SSL 设置

strict-ssl

  • 默认值:true
  • 类型:Boolean

通过 HTTPS 向registry发出请求时是否进行 SSL 密钥验证。

另请参阅 ca 配置项。

证书设置

ca

  • 默认值:npm CA 证书
  • 类型:String,Array 或 null

可信的用于注册源 SSL 链接的 CA 签名证书。 值应采用 PEM 格式(也称 “Base-64 encoded X.509 (.CER)”)。 例如:

ca="-----BEGIN CERTIFICATE-----\nXXXX\nXXXX\n-----END CERTIFICATE-----"

设置为 null 时仅允许已知注册商,若指定 CA 证书将只信任指定的证书颁发机构。

通过指定一个证书数组,可以信任多个 CA:

ca[]="..."
ca[]="..."

更多信息可见 strict-ssl 设置

cafile

  • 默认值: null
  • 类型:路径

包含一个或多个 CA 证书的文件路径。 类似于 ca 设置,但允许多个CA, 此外, CA 信息将存储在一个文件中,而不是通过 CLI 指定。

<URL>:CA文件

定义访问指定注册源时使用的证书颁发机构文件的路径。 例如:

//registry.npmjs.org/:cafile=ca-cert.pem

<URL>:ca

添加于:v10.25.0

为指定的注册源定义一个内联证书颁发机构证书。 该值必须采用 PEM 编码,就像全局 ca 设置一样,但它只对匹配的注册表 URL 适用 。

//registry.example.com/:ca=-----BEGIN CERTIFICATE-----...-----END CERTIFICATE-----

cert

  • 默认值: null
  • 类型:字符串

访问注册源时传递的客户端证书。 值应为 PEM 格式(也称 "Base-64 encoded X.509 (.CER)")。 例如:

cert="-----BEGIN CERTIFICATE-----\nXXXX\nXXXX\n-----END CERTIFICATE-----"

这不是证书文件的路径。

<URL>:cert

添加于:v10.25.0

定义一个内联客户端证书,以便在访问指定的注册源时使用。 示例:

//registry.example.com/:cert=-----BEGIN CERTIFICATE-----...-----END CERTIFICATE-----

<URL>:证书文件

定义访问指定注册源时使用的证书文件的路径。 例如:

//registry.npmjs.org/:certfile=server-cert.pem

key

  • 默认值: null
  • 类型:string

访问注册源时要传递的客户端密钥。 值应为 PEM 格式(也称 "Base-64 encoded X.509 (.CER)")。 例如:

key="-----BEGIN PRIVATE KEY-----\nXXXX\nXXXX\n-----END PRIVATE KEY-----"

这不是密钥文件的路径。 如果你需要引用文件系统而不是内嵌密钥,使用 <URL>&#58;#;密钥文件

此设置包含敏感信息。 不要将其写入本地会提交到仓库的 .npmrc 文件。

<URL>:key

添加于:v10.25.0

为指定的注册表 URL 定义一个内联客户端密钥。

//registry.example.com/:key=-----BEGIN PRIVATE KEY-----...-----END PRIVATE KEY-----

<URL>:密钥文件

定义访问指定注册源时使用的客户端密钥文件的路径。 例如:

//registry.npmjs.org/:keyfile=server-key.pem