设置 (.npmrc)
pnpm 从命令行、环境变量和 .npmrc
文件获取其配置。
¥pnpm gets its configuration from the command line, environment variables, and
.npmrc
files.
pnpm config
命令可用于更新和编辑用户和全局 .npmrc
文件的内容。
¥The pnpm config
command can be used to update and edit the contents of the
user and global .npmrc
files.
四个相关文件是:
¥The four relevant files are:
-
每个项目的配置文件 (
/path/to/my/project/.npmrc
)¥per-project configuration file (
/path/to/my/project/.npmrc
) -
每个工作区的配置文件(包含
pnpm-workspace.yaml
文件的目录)¥per-workspace configuration file (the directory that contains the
pnpm-workspace.yaml
file) -
每用户配置文件 (
~/.npmrc
)¥per-user configuration file (
~/.npmrc
) -
全局配置文件(
/etc/npmrc
)¥global configuration file (
/etc/npmrc
)
所有 .npmrc
文件都是 key = value
参数的 INI-formatted 列表。
¥All .npmrc
files are an INI-formatted list of key = value
parameters.
.npmrc
文件中的值可能包含使用 ${NAME}
语法的环境变量。环境变量也可以指定默认值。如果未设置 NAME
,则 使用 ${NAME-fallback}
将返回 fallback
。如果 NAME
未设置或者为空字符串,则 ${NAME:-fallback}
将返回 fallback
。
¥Values in the .npmrc
files may contain env variables using the ${NAME}
syntax. The env variables may also be specified with default values. 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.
依赖提升设置
¥Dependency Hoisting Settings
hoist
-
默认:true
¥Default: true
-
类型:boolean
¥Type: boolean
当 true
时,所有依赖都提升到 node_modules/.pnpm/node_modules
。这使得 node_modules
内的所有包都可以访问未列出的依赖。
¥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
-
默认:true
¥Default: true
-
类型:boolean
¥Type: boolean
当 true
时,工作区中的包将符号链接到 <workspace_root>/node_modules/.pnpm/node_modules
或 <workspace_root>/node_modules
,具体取决于其他提升设置(hoist-pattern
和 public-hoist-pattern
)。
¥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: ['*']
-
类型:string[]
¥Type: string[]
告诉 pnpm 哪些包应该提升到 node_modules/.pnpm/node_modules
。默认情况下,所有包都会被吊起 - 但是,如果你知道只有某些有缺陷的包具有幻像依赖,则可以使用此选项专门提升幻像依赖(推荐)。
¥Tells pnpm which packages should be hoisted to node_modules/.pnpm/node_modules
. By
default, all packages are hoisted - however, if you know that only some flawed
packages have phantom dependencies, you can use this option to exclusively hoist
the phantom dependencies (recommended).
例如:
¥For instance:
hoist-pattern[]=*eslint*
hoist-pattern[]=*babel*
你还可以使用 !
从提升中排除模式。
¥You may also exclude patterns from hoisting using !
.
例如:
¥For instance:
hoist-pattern[]=*types*
hoist-pattern[]=!@types/react
public-hoist-pattern
-
默认:['eslint', 'prettier']
¥Default: ['eslint', 'prettier']
-
类型:string[]
¥Type: string[]
与 hoist-pattern
将依赖提升到虚拟存储内的隐藏模块目录不同,public-hoist-pattern
将与模式匹配的依赖提升到根模块目录。提升到根模块目录意味着应用代码将有权访问幻像依赖,即使它们不正确地修改了解析策略。
¥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. Hoisting to the root modules
directory means that application code will have access to phantom dependencies,
even if they modify the resolution strategy improperly.
当处理一些有缺陷且无法正确解析依赖的可插入工具时,此设置非常有用。
¥This setting is useful when dealing with some flawed pluggable tools that don't resolve dependencies properly.
例如:
¥For instance:
public-hoist-pattern[]=*plugin*
注意:设置 shamefully-hoist
至 true
与设置 public-hoist-pattern
至 *
相同。
¥Note: Setting shamefully-hoist
to true
is the same as setting
public-hoist-pattern
to *
.
你还可以使用 !
从提升中排除模式。
¥You may also exclude patterns from hoisting using !
.
例如:
¥For instance:
public-hoist-pattern[]=*types*
public-hoist-pattern[]=!@types/react
shamefully-hoist
-
默认:false
¥Default: false
-
类型:布尔值
¥Type: Boolean
默认情况下,pnpm 创建半严格 node_modules
,这意味着依赖可以访问未声明的依赖,但 node_modules
之外的模块则不能。通过这种布局,生态系统中的大多数软件包都可以正常工作。但是,如果某些工具仅在提升的依赖位于 node_modules
的根目录中时才起作用,则可以将其设置为 true
来为你提升它们。
¥By default, pnpm creates a semistrict node_modules
, meaning dependencies have
access to undeclared dependencies but modules outside of node_modules
do not.
With this layout, most of the packages in the ecosystem work with no issues.
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 模块设置
¥Node-Modules Settings
modules-dir
-
默认:node_modules
¥Default: node_modules
-
类型:path
¥Type: path
将安装依赖的目录(而不是 node_modules
)。
¥The directory in which dependencies will be installed (instead of
node_modules
).
node-linker
-
默认:isolated
¥Default: isolated
-
类型:isolated, hoisted, pnp
¥Type: isolated, hoisted, pnp
定义应使用什么链接器来安装 Node 包。
¥Defines what linker should be used for installing Node packages.
-
isolated - 依赖从
node_modules/.pnpm
处的虚拟存储进行符号链接。¥isolated - dependencies are symlinked from a virtual store at
node_modules/.pnpm
. -
hoisted - 创建了一个没有符号链接的扁平
node_modules
。与 npm 或 Yarn Classic 创建的node_modules
相同。当使用此设置时,Yarn 的库之一用于提升。使用此设置的正当理由:¥hoisted - a flat
node_modules
without symlinks is created. Same as thenode_modules
created by npm or Yarn Classic. One of Yarn's libraries is used for hoisting, when this setting is used. Legitimate reasons to use this setting:-
你的工具不能很好地处理符号链接。React Native 项目很可能只有在使用提升的
node_modules
时才能工作。¥Your tooling doesn't work well with symlinks. A React Native project will most probably only work if you use a hoisted
node_modules
. -
你的项目已部署到无服务器托管提供商。某些无服务器提供商(例如 AWS Lambda)不支持符号链接 。此问题的另一种解决方案是在部署之前打包应用。
¥Your project is deployed to a serverless hosting provider. Some serverless providers (for instance, AWS Lambda) don't support symlinks. An alternative solution for this problem is to bundle your application before deployment.
-
如果你想用
"bundledDependencies"
发布你的包。¥If you want to publish your package with
"bundledDependencies"
. -
如果你正在使用 --preserve-symlinks 标志运行 Node.js。
¥If you are running Node.js with the --preserve-symlinks flag.
-
-
pnp - no
node_modules
。Plug'n'Play 是针对 Node 由 Yarn Berry 使用 的创新策略。当使用pnp
作为链接器时,建议将symlink
设置为false
。¥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
-
默认:true
¥Default: true
-
类型:布尔值
¥Type: Boolean
当 symlink
设置为 false
时,pnpm 将创建一个不带任何符号链接的虚拟存储目录。与 node-linker=pnp
一起使用是一个有用的设置。
¥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
-
默认:true
¥Default: true
-
类型:布尔值
¥Type: Boolean
当 false
时,pnpm 不会向模块目录(node_modules
)写入任何文件。当模块目录与用户空间(FUSE)中的文件系统一起安装时,这非常有用。有一个实验性的 CLI,允许你使用 FUSE 挂载模块目录:@pnpm/mount-modules。
¥When false
, pnpm will not write any files to the modules directory
(node_modules
). This is useful for when the modules directory is mounted with
filesystem in userspace (FUSE). There is an experimental CLI that allows you to
mount a modules directory with FUSE: @pnpm/mount-modules.
virtual-store-dir
-
默认:node_modules/.pnpm
¥Default: node_modules/.pnpm
-
类型:path
¥Types: path
包含存储链接的目录。项目的所有直接和间接依赖都链接到此目录中。
¥The directory with links to the store. All direct and indirect dependencies of the project are linked into this directory.
这是一个有用的设置,可以解决 Windows 上的长路径问题。如果你有一些路径很长的依赖,你可以在驱动器的根目录中选择一个虚拟存储(例如 C:\my-project-store
)。
¥This is a useful setting that can solve issues with long paths on 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
).
或者你可以将虚拟存储设置为 .pnpm
并将其添加到 .gitignore
。这将使堆栈跟踪更清晰,因为依赖的路径将高一个目录。
¥Or you can set the virtual store to .pnpm
and add it to .gitignore
. This
will make stacktraces cleaner as paths to dependencies will be one directory
higher.
注意:虚拟存储不能在多个项目之间共享。每个项目都应该有自己的虚拟存储(共享根目录的工作区除外)。
¥NOTE: the virtual store cannot be shared between several projects. Every project should have its own virtual store (except for in workspaces where the root is shared).
virtual-store-dir-max-length
添加于:v9.1.0
¥Added in: v9.1.0
-
默认:120
¥Default: 120
-
类型:number
¥Types: number
设置虚拟存储目录 (node_modules/.pnpm
) 内目录名称的最大允许长度。如果你在 Windows 上遇到长路径问题,可以将其设置为较低的数字。
¥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
-
默认:auto
¥Default: auto
-
类型:自动、硬链接、复制、克隆、克隆或复制
¥Type: auto, hardlink, copy, clone, clone-or-copy
控制从存储导入包的方式(如果要禁用 node_modules
内的符号链接,则需要更改 node-linker 设置,而不是此设置)。
¥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 - 尝试从存储克隆包。如果不支持克隆,则从存储硬链接包。如果克隆和链接都不可能,则退回到复制
¥auto - try to clone packages from the store. If cloning is not supported then hardlink packages from the store. If neither cloning nor linking is possible, fall back to copying
-
hardlink - 来自存储的硬链接包
¥hardlink - hard link packages from the store
-
clone-or-copy - 尝试从存储克隆包。如果不支持克隆,则退 回到复制
¥clone-or-copy - try to clone packages from the store. If cloning is not supported then fall back to copying
-
copy - 从存储复制包
¥copy - copy packages from the store
-
clone - 从存储克隆(又名写入时复制或参考链接)包
¥clone - clone (AKA copy-on-write or reference link) packages from the store
克隆是将包写入 node_modules 的最佳方法。这是最快的方法,也是最安全的方法。使用克隆时,你可以编辑 node_modules 中的文件,并且它们不会在中央内容可寻址存储中被修改。
¥Cloning is the best way to write packages to node_modules. It is the fastest way and safest way. When cloning is used, you may edit files in your node_modules and they will not be modified in the central content-addressable store.
不幸的是,并非所有文件系统都支持克隆。我们建议使用写时复制 (CoW) 文件系统(例如,Linux 上的 Btrfs 而不是 Ext4),以获得 pnpm 的最佳体验。
¥Unfortunately, not all file systems support cloning. We recommend using a copy-on-write (CoW) file system (for instance, Btrfs instead of Ext4 on Linux) for the best experience with pnpm.
modules-cache-max-age
-
默认:10080(7 天分钟)
¥Default: 10080 (7 days in minutes)
-
类型:number
¥Type: number
应从模块目录中删除孤立包的时间(以分钟为单位)。pnpm 在模块目录中保留包的缓存。这可以提高切换分支或降级依赖时的安装速度。
¥The time in minutes after which orphan packages from the modules directory should be removed. pnpm keeps a cache of packages in the modules directory. This boosts installation speed when switching branches or downgrading dependencies.
dlx-cache-max-age
-
默认:1440(1 天的分钟数)
¥Default: 1440 (1 day in minutes)
-
类型:number
¥Type: number
dlx 缓存过期的时间(以分钟为单位)。执行 dlx 命令后,pnpm 会保留一个缓存,该缓存会省略后续调用同一 dlx 命令的安装步骤。
¥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
-
默认:
¥Default:
-
如果设置了 $PNPM_HOME 环境变量,则 $PNPM_HOME/store
¥If the $PNPM_HOME env variable is set, then $PNPM_HOME/store
-
如果设置了 $XDG_DATA_HOME 环境变量,则 $XDG_DATA_HOME/pnpm/store
¥If the $XDG_DATA_HOME env variable is set, then $XDG_DATA_HOME/pnpm/store
-
在 Windows 上:~/AppData/Local/pnpm/store
¥On Windows: ~/AppData/Local/pnpm/store
-
在 macOS 上:~/Library/pnpm/store
¥On macOS: ~/Library/pnpm/store
-
在 Linux 上:~/.local/share/pnpm/store
¥On Linux: ~/.local/share/pnpm/store
-
-
类型:path
¥Type: path
所有包在磁盘上的保存位置。
¥The location where all the packages are saved on the disk.
该存储应始终位于进行安装的同一磁盘上,因此每个磁盘将有一个存储。如果当前磁盘上有主目录,则在其中创建存储。如果磁盘上没有主目录,则在文件系统的根目录下创建存储。例如,如果安装发生在安装在 /mnt
的文件系统上,则存储将在 /mnt/.pnpm-store
创建。Windows 系统也是如此。
¥The store should be always on the same disk on which installation is happening,
so there will be one store per disk. If there is a home directory on the current
disk, then the store is created inside it. If there is no home on the disk,
then the store is created at the root of the filesystem. For
example, if installation is happening on a filesystem mounted at /mnt
,
then the store will be created at /mnt/.pnpm-store
. The same goes for Windows
systems.
可以从不同的磁盘设置存储,但在这种情况下,pnpm 将从存储中复制包而不是硬链接它们,因为硬链接只能在同一文件系统上使用。
¥It is possible to set a store from a different disk but in that case pnpm will copy packages from the store instead of hard-linking them, as hard links are only possible on the same filesystem.
verify-store-integrity
-
默认:true
¥Default: true
-
类型:布尔值
¥Type: Boolean
默认情况下,如果存储中的文件已被修改,则在将其链接到项目的 node_modules
之前会检查该文件的内容。如果 verify-store-integrity
设置为 false
,则在安装过程中不会检查内容寻址存储中的文件。
¥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
-
默认:false
¥Default: false
-
类型:布尔值
¥Type: Boolean
仅允许使用存储服务器进行安装。如果没有正在运行的存储服务器,安装将失败。
¥Only allows installation with a store server. If no store server is running, installation will fail.
strict-store-pkg-content-check
添加于:v9.4.0
¥Added in: v9.4.0
-
默认:true
¥Default: true
-
类型:布尔值
¥Type: Boolean
某些注册表允许在不同的包名称和/或版本下发布完全相同的内容。这会破坏存储中软件包的有效性检查。为了避免在存储中验证此类软件包的名称和版本时出现错误,你可以将 strict-store-pkg-content-check
设置设置为 false
。
¥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 Settings
lockfile
-
默认:true
¥Default: true
-
类型:布尔值
¥Type: Boolean
当设置为 false
时,pnpm 将不会读取或生成 pnpm-lock.yaml
文件。
¥When set to false
, pnpm won't read or generate a pnpm-lock.yaml
file.
prefer-frozen-lockfile
-
默认:true
¥Default: true
-
类型:布尔值
¥Type: Boolean
当设置为 true
并且可用的 pnpm-lock.yaml
满足 package.json
依赖指令时,将执行无头安装。无头安装会跳过所有依赖解析,因为它不需要修改锁定文件。
¥When set to true
and the available pnpm-lock.yaml
satisfies the
package.json
dependencies directive, a headless installation is performed. A
headless installation skips all dependency resolution as it does not need to
modify the lockfile.
lockfile-include-tarball-url
-
默认:false
¥Default: false
-
类型:布尔值
¥Type: Boolean
将软件包 tarball 的完整 URL 添加到 pnpm-lock.yaml
中的每个条目。
¥Add the full URL to the package's tarball to every entry in pnpm-lock.yaml
.
git-branch-lockfile
-
默认:false
¥Default: false
-
类型:布尔值
¥Type: Boolean
当设置为 true
时, 安装后生成的锁定文件名称将根据当前分支名称命名,以完全避免合并冲突。例如,如果当前分支名称为 feature-foo
,则对应的锁文件名称将为 pnpm-lock.feature-foo.yaml
,而不是 pnpm-lock.yaml
。它通常与命令行参数 --merge-git-branch-lockfiles
结合使用,或者通过在 .npmrc
文件中设置 merge-git-branch-lockfiles-branch-pattern
来使用。
¥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
-
默认:null
¥Default: null
-
类型:数组或空值
¥Type: Array or null
该配置匹配当前分支名称来确定是否合并所有 git 分支 lockfile 文件。默认情况下,需要手动传递 --merge-git-branch-lockfiles
命令行参数。此配置允许该过程自动补齐。
¥This configuration matches the current branch name to determine whether to merge
all git branch lockfile files. By default, you need to manually pass the
--merge-git-branch-lockfiles
command line parameter. This configuration allows
this process to be automatically completed.
例如:
¥For instance:
merge-git-branch-lockfiles-branch-pattern[]=main
merge-git-branch-lockfiles-branch-pattern[]=release*
你还可以使用 !
排除模式。
¥You may also exclude patterns using !
.
peers-suffix-max-length
添加于:v9.3.0
¥Added in: v9.3.0
-
默认:1000
¥Default: 1000
-
类型:number
¥Type: number
添加到锁文件中依赖密钥的对等 ID 后缀的最大长度。如果后缀较长,则用哈希替换。