Settings (.npmrc)
pnpm gets its configuration from the command line, environment variables, and
.npmrc
files.
The pnpm config
command can be used to update and edit the contents of the
user and global .npmrc
files.
四个相关文件分别为:
- per-project configuration file (
/path/to/my/project/.npmrc
) - per-workspace configuration file (the directory that contains the
pnpm-workspace.yaml
file) - per-user configuration file (
~/.npmrc
) - global configuration file (
/etc/npmrc
)
All .npmrc
files are an INI-formatted list of key = value
parameters.
Values in the .npmrc
files may contain env variables using the ${NAME}
syntax. 也可以使用默认值指定环境变量。 Using ${NAME-fallback}
will return fallback
if NAME
isn't set. ${NAME:-fallback}
will return fallback
if NAME
isn't set, or is an empty string.
依赖提升设置
hoist
- Default: true
- Type: boolean
When true
, all dependencies are hoisted to node_modules/.pnpm/node_modules
. This makes
unlisted dependencies accessible to all packages inside node_modules
.
hoist-workspace-packages
- Default: true
- Type: boolean
When true
, packages from the workspaces are symlinked to either <workspace_root>/node_modules/.pnpm/node_modules
or to <workspace_root>/node_modules
depending on other hoisting settings (hoist-pattern
and public-hoist-pattern
).
hoist-pattern
- Default: ['*']
- Type: string[]
Tells pnpm which packages should be hoisted to node_modules/.pnpm/node_modules
. 默认情况下,所有包都被提升 —— 但是,如果您知道只有某些有缺陷的包具有幻影依赖,您可以使用此选项专门提升幻影依赖(推荐做法)。
例如:
hoist-pattern[]=*eslint*
hoist-pattern[]=*babel*
You may also exclude patterns from hoisting using !
.
例如:
hoist-pattern[]=*types*
hoist-pattern[]=!@types/react
public-hoist-pattern
- Default: ['*eslint*', '*prettier*']
- Type: string[]
Unlike hoist-pattern
, which hoists dependencies to a hidden modules directory
inside the virtual store, public-hoist-pattern
hoists dependencies matching
the pattern to the root modules directory. 提升至根模块目录中意味着应用代码可以访问到幻影依赖,即使他们对解析策略做了不当的修改。
当处理一些有缺陷的插件不能正确解析依赖关系时,此设置很有用。
例如:
public-hoist-pattern[]=*plugin*
Note: Setting shamefully-hoist
to true
is the same as setting
public-hoist-pattern
to *
.
You may also exclude patterns from hoisting using !
.
例如:
public-hoist-pattern[]=*types*
public-hoist-pattern[]=!@types/react
shamefully-hoist
- Default: false
- Type: Boolean
By default, pnpm creates a semistrict node_modules
, meaning dependencies have
access to undeclared dependencies but modules outside of node_modules
do not.
通过这种布局,生态系统中的大多数的包都可以正常工作。
However, if some tooling only works when the hoisted dependencies are in the
root of node_modules
, you can set this to true
to hoist them for you.
Node 模块设置
modules-dir
- Default: node_modules
- Type: path
The directory in which dependencies will be installed (instead of
node_modules
).
node-linker
- Default: isolated
- Type: isolated, hoisted, pnp
定义应该使用什么链接器来安装 Node 包。
- isolated - dependencies are symlinked from a virtual store at
node_modules/.pnpm
. - hoisted - a flat
node_modules
without symlinks is created. Same as thenode_modules
created by npm or Yarn Classic. 当使用此设置时,Yarn 的一个库用于提升。 使用此设置的正当理由:- 您的工具不适用于符号链接。 A React Native project will most probably only work if you use a hoisted
node_modules
. - 您的项目会被部署到 serverless 服务提供商。 一些 serverless 提供商(例如 AWS Lambda)不支持符号链接。 此问题的另一种解决方案是在部署之前打包您的应用程序。
- If you want to publish your package with
"bundledDependencies"
. - If you are running Node.js with the --preserve-symlinks flag.
- 您的工具不适用于符号链接。 A React Native project will most probably only work if you use a hoisted
- pnp - no
node_modules
. Plug'n'Play is an innovative strategy for Node that is used by Yarn Berry. It is recommended to also setsymlink
setting tofalse
when usingpnp
as your linker.
symlink
- Default: true
- Type: Boolean
When symlink
is set to false
, pnpm creates a virtual store directory without
any symlinks. It is a useful setting together with node-linker=pnp
.
enable-modules-dir
- Default: true
- Type: Boolean
When false
, pnpm will not write any files to the modules directory
(node_modules
). 这对于在用户空间的文件系统 (FUSE) 中挂载模块目录时很有用。 There is an experimental CLI that allows you to
mount a modules directory with FUSE: @pnpm/mount-modules.
virtual-store-dir
- Default: node_modules/.pnpm
- Types: path
带有指向存储的链接的目录。 所有直接和间接依赖项都链接到此目录中。
这是一个有用的设置,可以解决 Windows 上长路径的问题。 If
you have some dependencies with very long paths, you can select a virtual store
in the root of your drive (for instance C:\my-project-store
).
Or you can set the virtual store to .pnpm
and add it to .gitignore
. 这将使堆栈跟踪更清晰,因为依赖项的路径将会提高一个目录层级。
NOTE: the virtual store cannot be shared between several projects. 每个项目都应该有自己的虚拟存储(除了在工作空间中被共享的根目录)。
virtual-store-dir-max-length
Added in: v9.1.0
- Default: 120
- Types: number
Sets the maximum allowed length of directory names inside the virtual store directory (node_modules/.pnpm
). You may set this to a lower number if you encounter long path issues on Windows.
package-import-method
- Default: auto
- Type: auto, hardlink, copy, clone, clone-or-copy
Controls the way packages are imported from the store (if you want to disable symlinks inside node_modules
, then you need to change the node-linker setting, not this one).
- auto - try to clone packages from the store. 如果不支持克隆则从存储硬链接包。 如果克隆和链接都不支持,则回退到复制
- hardlink - hard link packages from the store
- clone-or-copy - try to clone packages from the store. 如果不支持克隆则回退到复制。
- copy - copy packages from the store
- clone - clone (AKA copy-on-write or reference link) packages from the store
克隆是将包写入 node_modules 的最佳方式。 这是最快的方式,也是最安全的方式。 使用克隆时,您可以编辑 node_modules 中的文件,并且不会在中央内容可寻址存储中修改它们。
不幸的是,并非所有文件系统都支持克隆。 我们建议使用写时复制 (CoW) 文件系统(例如,在 Linux 上使用 Btrfs 而不是 Ext4)以获得最佳的 pnpm 体验。
modules-cache-max-age
- Default: 10080 (7 days in minutes)
- Type: number
孤立包应该从模块目录中被删除的时间(以分钟为单位)。 pnpm 在模块目录中保存了一个包的缓存。 切换分支或降级依赖项时,这会提高安装速度。
dlx-cache-max-age
- Default: 1440 (1 day in minutes)
- Type: number
The time in minutes after which dlx cache expires. After executing a dlx command, pnpm keeps a cache that omits the installation step for subsequent calls to the same dlx command.
Store Settings
store-dir
- 默认值:
- If the $PNPM_HOME env variable is set, then $PNPM_HOME/store
- If the $XDG_DATA_HOME env variable is set, then $XDG_DATA_HOME/pnpm/store
- On Windows: ~/AppData/Local/pnpm/store
- On macOS: ~/Library/pnpm/store
- On Linux: ~/.local/share/pnpm/store
- Type: path
所有包被保存在磁盘上的位置。
该存储应始终位于进行安装的同一磁盘上,因此每个磁盘将有一个存储。 如果在使用磁盘中具有主目录,存储目录就会创建在这里。 如果磁盘上没有主目录,那么将在文件系统的根目录中创建该存储。 For
example, if installation is happening on a filesystem mounted at /mnt
,
then the store will be created at /mnt/.pnpm-store
. Windows 系统上也是如此。
可以从不同的磁盘设置同一个存储,但在这种情况下,pnpm 将复制包而不是硬链接它们,因为硬链接只能发生在同一文件系统上。
verify-store-integrity
- Default: true
- Type: Boolean
By default, if a file in the store has been modified, the content of this file is checked before linking it to a project's node_modules
. If verify-store-integrity
is set to false
, files in the content-addressable store will not be checked during installation.
use-running-store-server
Deprecated feature
- Default: false
- Type: Boolean
只允许使用存储服务器进行安装。 如果存储服务器没有在运行,安装将失败。
strict-store-pkg-content-check
Added in: v9.4.0
- Default: true
- Type: Boolean
Some registries allow the exact same content to be published under different package names and/or versions. This breaks the validity checks of packages in the store. To avoid errors when verifying the names and versions of such packages in the store, you may set the strict-store-pkg-content-check
setting to false
.
锁文件设置
lockfile
- Default: true
- Type: Boolean
When set to false
, pnpm won't read or generate a pnpm-lock.yaml
file.
prefer-frozen-lockfile
- Default: true
- Type: Boolean
When set to true
and the available pnpm-lock.yaml
satisfies the
package.json
dependencies directive, a headless installation is performed. 无头安装会跳过所有依赖项解析,因为它不需要修改lockfile。
lockfile-include-tarball-url
- Default: false
- Type: Boolean
Add the full URL to the package's tarball to every entry in pnpm-lock.yaml
.
git-branch-lockfile
- Default: false
- Type: Boolean
When set to true
, the generated lockfile name after installation will be named
based on the current branch name to completely avoid merge conflicts. For example,
if the current branch name is feature-foo
, the corresponding lockfile name will
be pnpm-lock.feature-foo.yaml
instead of pnpm-lock.yaml
. It is typically used
in conjunction with the command line argument --merge-git-branch-lockfiles
or by
setting merge-git-branch-lockfiles-branch-pattern
in the .npmrc
file.
merge-git-branch-lockfiles-branch-pattern
- Default: null
- Type: Array or null
此配置匹配当前分支名称以确定是否合并所有 git 分支锁文件文件。 By default, you need to manually pass the
--merge-git-branch-lockfiles
command line parameter. 这项配置允许自动完成这个过程。
例如:
merge-git-branch-lockfiles-branch-pattern[]=main
merge-git-branch-lockfiles-branch-pattern[]=release*
You may also exclude patterns using !
.
peers-suffix-max-length
Added in: v9.3.0
- Default: 1000
- Type: number
Max length of the peer IDs suffix added to dependency keys in the lockfile. If the suffix is longer, it is replaced with a hash.
Registry & Authentication Settings
registry
- Default: https://registry.npmjs.org/
- Type: url
npm 包注册表的基准 URL(包括尾部斜杠)。
<scope>:registry
The npm registry that should be used for packages of the specified scope. For
example, setting @babel:registry=https://example.com/packages/npm/
will enforce that when you use pnpm add @babel/core
, or any @babel
scoped
package, the package will be fetched from https://example.com/packages/npm
instead of the default registry.
<URL>:_authToken
Define the authentication bearer token to use when accessing the specified registry. 示例:
//registry.npmjs.org/:_authToken=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
您还可以使用环境变量。 示例:
//registry.npmjs.org/:_authToken=${NPM_TOKEN}
Or you may just use an environment variable directly, without changing .npmrc
at all:
npm_config_//registry.npmjs.org/:_authToken=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
<URL>:tokenHelper
A token helper is an executable which outputs an auth token. This can be used in situations where the authToken is not a constant value but is something that refreshes regularly, where a script or other tool can use an existing refresh token to obtain a new access token.
The configuration for the path to the helper must be an absolute path, with no arguments. In order to be secure, it is only permitted to set this value in the user .npmrc
. Otherwise a project could place a value in a project's local .npmrc
and run arbitrary executables.
Setting a token helper for the default registry:
tokenHelper=/home/ivan/token-generator
Setting a token helper for the specified registry:
//registry.corp.com:tokenHelper=/home/ivan/token-generator
请求设置
ca
- Default: The npm CA certificate
- Type: String, Array or null
为与源服务器进行可信的 SSL 链接颁发的 CA 证书。 值应采用 PEM 格式(也称 “Base-64 encoded X.509 (.CER)”)。 示例:
ca="-----BEGIN CERTIFICATE-----\nXXXX\nXXXX\n-----END CERTIFICATE-----"
设置为 null 时仅允许已知注册商,若指定 CA 证书将只信任指定的证书颁发机构。
通过指定一个证书数组,可以信任多个 CA:
ca[]="..."
ca[]="..."
See also the strict-ssl
config.
cafile
- Default: null
- Type: path
包含一个或多个 CA 证书的文件路径。 Similar to the ca
setting, but allows for multiple CAs, as well
as for the CA information to be stored in a file instead of being specified via
CLI.
<URL>:cafile
Define the path to a Certificate Authority file to use when accessing the specified registry. 示例:
//registry.npmjs.org/:keyfile=client-cert.pem
cert
- Default: null
- Type: String
访问注册源时传递的客户端证书。 值应为 PEM 格式(也称 "Base-64 encoded X.509 (.CER)")。 示例:
cert="-----BEGIN CERTIFICATE-----\nXXXX\nXXXX\n-----END CERTIFICATE-----"
It is not the path to a certificate file.
<URL>:certfile
Define the path to a certificate file to use when accessing the specified registry. 示例:
//registry.npmjs.org/:certfile=server-cert.pem
key
- Default: null
- Type: String
访问注册源时要传递的客户端密钥。 值应采用 PEM 格式(AKA “Base-64 encoded X.509 (.CER)”)。 示例:
key="-----BEGIN PRIVATE KEY-----\nXXXX\nXXXX\n-----END PRIVATE KEY-----"
It is not the path to a key file (and there is no keyfile
option).
这个设置含有敏感信息! Don't write it to a local .npmrc
file committed to the repository.
<URL>:keyfile
Define the path to a client key file to use when accessing the specified registry. 示例:
//registry.npmjs.org/:keyfile=server-key.pem
git-shallow-hosts
- Default: ['github.com', 'gist.github.com', 'gitlab.com', 'bitbucket.com', 'bitbucket.org']
- Type: string[]
当获取 Git 仓库中的依赖项时,如果域名在此设置中列出,pnpm 将使用浅克隆仅获取所需的提交,而不是所有历史记录。
https-proxy
- Default: null
- Type: url
用于传输 HTTPS 请求的代理。 If the HTTPS_PROXY
, https_proxy
,
HTTP_PROXY
or http_proxy
environment variables are set, their values will be
used instead.
如果您的代理 URL 包含用户名和密码,请确保对它们进行 URL 编码。 例如:
https-proxy=https://use%21r:pas%2As@my.proxy:1234/foo
Do not encode the colon (:
) between the username and password.
http-proxy
proxy
- Default: null
- Type: url
用于传出 http 请求的代理。 如果设置了 HTTP_PROXY 或 http_proxy 环境变量,则底层请求库将遵循代理设置。
local-address
- Default: undefined
- Type: IP Address
连接到 npm registry 时要使用的本地接口 IP 地址。
maxsockets
- Default: network-concurrency x 3
- Type: Number
每个源使用的最大连接数(协议/主机/端口组合)。
noproxy
- Default: null
- Type: String
一个由逗号分割的域名字符串,表示不应该被使用的代理
strict-ssl
- Default: true
- Type: Boolean
通过 HTTPS 向registry发出请求时是否进行 SSL 密钥验证。
See also the ca
option.
network-concurrency
- Default: 16
- Type: Number
控制同时处理的最大 HTTP(S) 的网络请求数。
fetch-retries
- Default: 2
- Type: Number
如果 pnpm 无法从registry中获取,重试次数。
fetch-retry-factor
- Default: 10
- Type: Number
重试回退的指数因子。
fetch-retry-mintimeout
- Default: 10000 (10 seconds)
- Type: Number
重试请求的最小(基本)超时。
fetch-retry-maxtimeout
- Default: 60000 (1 minute)
- Type: Number
最大回退超时时间,以确保重试因子不会使请求时间过长。
fetch-timeout
- Default: 60000 (1 minute)
- Type: Number
等待 HTTP 请求完成的最长时间。
Peer Dependency 设置
auto-install-peers
- Default: true
- Type: Boolean
When true
, any missing non-optional peer dependencies are automatically installed.
版本冲突
如果来自不同软件包的对等依赖项的需求版本存在冲突,那么 pnpm 将不会自动安装任何版本的冲突的对等依赖项。 相反,会输出一条警告信息。 For example, if one dependency requires react@^16.0.0
and another requires react@^17.0.0
, these requirements conflict, and no automatic installation will occur.