认证设置
本页介绍注册源凭据和客户端证书。这些设置包含机密信息。请勿将它们放入你的仓库中。
For other settings, such as proxies and registries, see Settings (pnpm-workspace.yaml).
快速开始#
在你的机器上,登录到注册源:
pnpm login
pnpm saves the token in <pnpm config>/auth.ini.
在 CI 中,通过环境变量传递令牌:
export pnpm_config__auth='{"https://registry.npmjs.org":{"@":{"authToken":"${NPM_TOKEN}"}}}'
认证文件位置#
pnpm 从两类文件中读取认证设置。
INI files in npm's .npmrc format hold the
.npmrc and auth.ini settings and the
certificate settings. 如果某个键在多个文件中设置,则此列表中的第一个文件优先:
<workspace root>/.npmrc, the project file. Add it to.gitignore.<pnpm config>/auth.ini.pnpm loginandpnpm config setwrite auth keys here.~/.npmrc, so credentials you set up for npm keep working. To read a different file, use thenpmrcAuthFilesetting.
The global <pnpm config>/config.yaml holds the _auth setting.
<pnpm config> 目录是:
$PNPM_CONFIG_HOME/, ifPNPM_CONFIG_HOMEis non-empty- Otherwise,
$XDG_CONFIG_HOME/pnpm/, ifXDG_CONFIG_HOMEis set ~/AppData/Local/pnpm/config/on Windows~/Library/Preferences/pnpm/on macOS~/.config/pnpm/on Linux
Settings in .npmrc and auth.ini#
Set these in one of the INI files listed in Auth file locations.
<URL>:_authToken#
The bearer token for a registry:
//registry.npmjs.org/:_authToken=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
In a user-level auth file, the value can reference an environment variable:
//registry.npmjs.org/:_authToken=${NPM_TOKEN}
请参阅 身份验证设置中的环境变量。
特定作用域的认证令牌#
Some registries, such as GitHub Packages, issue a separate token for each scope. Put the scope after the registry URL in the key:
@org-a:registry=https://npm.pkg.github.com/
@org-b:registry=https://npm.pkg.github.com/
//npm.pkg.github.com/:@org-a:_authToken=ORG_A_TOKEN
//npm.pkg.github.com/:@org-b:_authToken=ORG_B_TOKEN
//npm.pkg.github.com/:_authToken=FALLBACK_TOKEN
pnpm sends ORG_A_TOKEN for @org-a/* packages and ORG_B_TOKEN for
@org-b/* packages. Other packages from that registry get FALLBACK_TOKEN,
if it is set.
pnpm login --registry=https://npm.pkg.github.com --scope=@org-a saves a
scoped token in auth.ini.
<URL>:tokenHelper#
The path to a program that prints a token. Use it for tokens that expire, so a script can fetch a fresh one.
pnpm runs the program and sends its output as Authorization: Bearer <output>.
If the output already starts with a word and a space, such as Bearer abc,
Basic abc, or Token abc, pnpm sends it unchanged.
The path must be absolute and take no arguments. pnpm reads tokenHelper only
from user-level auth files. A project .npmrc could otherwise make pnpm run
any program. Environment variables cannot set it either.
For the default registry:
tokenHelper=/home/ivan/token-generator
For a specific registry:
//registry.corp.com:tokenHelper=/home/ivan/token-generator
Settings in config.yaml#
_auth#
All registry tokens in one value, keyed by registry URL. Use it in CI, where
the //host/:_authToken key cannot be an environment variable name on some
runners.
pnpm reads _auth from two places only:
- the global
config.yaml - the
pnpm_config__authenvironment variable, as JSON
pnpm ignores _auth in pnpm-workspace.yaml and .npmrc.
在全局 config.yaml 中:
_auth:
https://registry.npmjs.org:
"@":
authToken: npm-token
"@org":
authToken: org-token
The same value as an environment variable:
export pnpm_config__auth='{"https://registry.npmjs.org":{"@":{"authToken":"npm-token"},"@org":{"authToken":"org-token"}}}'
pnpm also reads PNPM_CONFIG__AUTH, the all-caps name some CI runners use. If
both are set, the lowercase one wins unless it is empty.
The value follows these rules:
- Each top-level key is an
httporhttpsregistry URL, with no credentials, query string, or fragment. - Under a URL,
"@"holds the token for the whole registry."@org"holds the token for packages in the@orgscope. authTokenis the only credential field. 它是一个 bearer 令牌。authTokencan reference an environment variable as${NAME}.
每个条目还会把包路由到其 URL。 "@" makes the URL the default
registry, and "@org" sends @org/* packages there. 此路由的优先级取决于条目的来源:
--registryand--@scope:registryon the command line win.- Entries from
pnpm_config__authoverride theregistryandregistriessettings. CI can use this to force a proxy registry. - Entries from the global
config.yamlapply only where noregistryorregistriessetting chooses a registry. The token is still sent to its own URL.
From v12.1.0 to v12.15.0, pnpm login saved tokens here. Since v12.16.0 it
saves them in auth.ini and removes the token it replaces from _auth.
pnpm logout removes tokens from both files.
认证设置中的环境变量#
User-level auth files, <pnpm config>/auth.ini and ~/.npmrc, can reference
environment variables as ${NAME}:
//registry.npmjs.org/:_authToken=${NPM_TOKEN}
pnpm does not expand environment variables in the project .npmrc for:
- registry and proxy URLs:
registry,@scope:registry,proxy,https-proxy,http-proxy - keys that start with
// - credentials:
_authToken,_auth,_password,username,tokenHelper,cert,key - TLS file paths:
cafile,<URL>:cafile,<URL>:certfile,<URL>:keyfile
pnpm 会忽略此类设置并输出警告。 A registry or
@scope:registry URL with a placeholder fails the command instead, because
ignoring it would resolve packages from another registry. The project .npmrc comes
with the repository, so a malicious repository could otherwise send your CI
tokens to a registry it controls
(GHSA-3qhv-2rgh-x77r).
将 token 移出项目 .npmrc#
If your repository commits a line such as
//registry.npmjs.org/:_authToken=${NPM_TOKEN}, use one of these instead.
通过以该键命名的环境变量传入 token:
env "pnpm_config_//registry.npmjs.org/:_authToken=$NPM_TOKEN" pnpm install
Shells cannot export this name, because it contains /, :, and .. Use
env as above, or set the variable in your CI provider's settings. It
overrides the project .npmrc, and a command-line option overrides it.
Or use _auth, as in the quick start.
Or save the token in <pnpm config>/auth.ini in a CI step:
pnpm config set //registry.npmjs.org/:_authToken "$NPM_TOKEN"
Or move the ${NPM_TOKEN} line to ~/.npmrc, or to the file that
npmrcAuthFile names.
In GitHub Actions, actions/setup-node with the registry-url input already
works. It writes a user-level .npmrc and points NPM_CONFIG_USERCONFIG at
it. pnpm reads that file, so NODE_AUTH_TOKEN keeps working.
If you cannot change every CI pipeline, mark the project .npmrc as trusted
with one variable in the CI environment:
PNPM_CONFIG_NPMRC_AUTH_FILE=.npmrc
This sets npmrcAuthFile. pnpm then reads
the project .npmrc as the user-level auth file and expands its variables. 相对路径会相对于工作目录解析。仓库无法设置此变量,因为它来自环境。 pnpm also reads
npm's NPM_CONFIG_USERCONFIG if this variable is unset.
警告
仅在你构建的每个仓库都可信的情况下使用此项。 It turns off this
protection for the checked-out repository, including the rule that only
user-level files can set tokenHelper.
这同样适用于由环境变量构建的注册源 URL。 Move it
to ~/.npmrc or set it with pnpm config set "<key>" <value>. A URL that is
not secret can be written out in the project .npmrc. For registries in
pnpm-workspace.yaml, see
registries.
证书设置#
Relative TLS file paths resolve from the directory containing the file that declares them.
Like the token settings, these go in .npmrc or auth.ini.
ca#
- Default: null (the system trust store)
- 类型:String,Array 或 null
用于与注册源建立 TLS 连接时要信任的证书颁发机构(CA)证书,PEM 格式:
ca="-----BEGIN CERTIFICATE-----\nXXXX\nXXXX\n-----END CERTIFICATE-----"
要信任多个 CA,请使用数组:
ca[]="..."
ca[]="..."
A ca or cafile replaces the default trusted CAs. When it is null, pnpm
trusts the CAs in the operating system's trust store. 如果 pnpm 无法加载
系统信任存储,它会信任 pnpm 内置的 Mozilla CA 集合。
See also strictSsl.
cafile#
- 默认值: null
- 类型:路径
包含一个或多个 CA 证书的文件的路径。 Use it instead of ca to
keep the certificates in a file.
NODE_EXTRA_CA_CERTS#
指向包含额外 CA 证书的 PEM 文件的路径,以环境变量形式设置。 pnpm adds them to the trusted CAs: the system trust store, or ca or
cafile when one is set. pnpm 会忽略缺失或无法读取的文件。
<URL>:ca#
PEM 格式的 CA 证书,仅对给定的注册源受信任:
//registry.example.com/:ca=-----BEGIN CERTIFICATE-----...-----END CERTIFICATE-----
<URL>:CA文件#
给定注册源的 CA 证书文件路径:
//registry.npmjs.org/:cafile=ca-cert.pem
cert#
- 默认值: null
- 类型:字符串
发送给注册源的客户端证书,PEM 格式。该值是证书本身,而不是路径:
cert="-----BEGIN CERTIFICATE-----\nXXXX\nXXXX\n-----END CERTIFICATE-----"
<URL>:cert#
发送给给定注册源的客户端证书:
//registry.example.com/:cert=-----BEGIN CERTIFICATE-----...-----END CERTIFICATE-----
<URL>:证书文件#
给定注册源的客户端证书文件路径:
//registry.npmjs.org/:certfile=server-cert.pem
key#
- 默认值: null
- 类型:字符串
发送给注册源的客户端密钥,PEM 格式。该值是密钥本身,而不是路径。 To read the key from a file, use <URL>:keyfile.
key="-----BEGIN PRIVATE KEY-----\nXXXX\nXXXX\n-----END PRIVATE KEY-----"
<URL>:key#
给定注册源的客户端密钥:
//registry.example.com/:key=-----BEGIN PRIVATE KEY-----...-----END PRIVATE KEY-----
<URL>:密钥文件#
给定注册源的客户端密钥文件路径:
//registry.npmjs.org/:keyfile=server-key.pem