跳到主内容
Version: 6.x

.npmrc

pnpm 从命令行、环境变量和 .npmrc 文件中获取其配置。

pnpm config 命令可用于更新和编辑 用户和全局 .npmrc 文件的内容。

四个相关文件分别为:

  • 每个项目的配置文件(/path/to/my/project/.npmrc
  • 每个工作区的配置文件(包含 pnpm-workspace.yaml 文件的目录)
  • 每位用户的配置文件( ~/.npmrc
  • 全局配置文件( /etc/npmrc

所有 .npmrc 文件都遵循 INI-formatted 列表,包含 key = value 参数。

依赖提升设置

hoist

添加于:v4.0.0

  • 默认值: true
  • 类型: boolean

true,所有依赖项都被提升到 node_modules/.pnpm。 这使得 node_modules所有包都可以访问 未列出的依赖项。

hoist-pattern

添加于:v4.0.0

  • 默认值: ['*']
  • 类型: string[]

告诉 pnpm 哪些包应该被提升到 node_modules/.pnpm。 在默认情况下,所有的包都是被提升的,但是如果你知道只有一些有缺陷的包有幻影依赖关系, 您可以使用此选项来只提升有幻影依赖关系的包(推荐)。

例如:

hoist-pattern[]=*eslint*
hoist-pattern[]=*babel*

public-hoist-pattern

添加于:v5.2.0

  • 默认值: ['*types*', '*eslint*', '@prettier/plugin-*', '*prettier-plugin-*']
  • 类型: string[]

不同于 hoist-pattern 会把依赖提升到一个虚拟存储中的隐藏的模块目录中,public-hoist-pattern 将匹配的依赖提升至根模块目录中。 提升至根模块目录中意味着应用代码可以访问到幻影依赖,即使他们对解析策略做了不当的修改。

当处理一些有缺陷的可插拔工具不能正确解析依赖关系时,此设置很有用。

例如:

public-hoist-pattern[]=*plugin*

注意:设置 shamefully-hoisttrue 与设置 public-hoist-pattern* 是一样的。

shamefully-hoist

添加于:v1.34.0 为 shamefully-flatten,在 v4.0.0 中重命名

  • 默认值: false
  • 类型:Boolean

默认情况下,pnpm 创建一个半严格的 node_modules,这意味着依赖项可以访问未声明的依赖项,但 node_modules 之外的模块不行。 通过这种布局,生态系统中的大多数的包都可以正常工作。 但是,如果某些工具仅在提升的依赖项位于根目录的 node_modules 时才有效,您可以将其设置为 true 来为您提升它们。

Node 模块设置

store-dir

添加于: v4.2.0 作为 store

  • 默认值:~/.pnpm-store
  • 类型:path

所有包被保存在磁盘上的位置。

该存储应始终位于进行安装的同一磁盘上,因此每个磁盘将有一个存储。 如果磁盘上存在主目录,存储则会被创建在 <home dir>/.pnpm-store。 如果磁盘上没有主目录,那么将在文件系统的根目录中创建该存储。 例如,如果安装发生在挂载在 /mnt 的文件系统上,那么存储将在 /mnt/.pnpm-store 处创建。 Windows 系统上也是如此。

可以从不同的磁盘设置同一个存储,但在这种情况下,pnpm 将复制包而不是硬链接它们,因为硬链接只能发生在同一文件系统上。

modules-dir

添加于:v4.14.0

  • 默认值:node_modules
  • 类型:path

将安装依赖项的目录(而不是 node_modules)。

node-linker

添加于:v5.9.0

  • 默认值:isolated
  • 类型: isolated, hoisted, pnp

定义应该使用什么链接器来安装 Node 包。

  • isolated - 依赖项从虚拟存储 node_modules/.pnpm 中建立符号链接
  • hoisted - 创建一个没有符号链接的扁平的 node_modules。 与 npm 或 Yarn Classic 创建 node_modules 一致。 使用此设置的正当理由:
    1. 您的工具不适用于符号链接。 React Native 项目很可能只有在你使用提升的 node_modules 才能工作。
    2. 您的项目会被部署到 serverless 服务提供商。 一些 serverless 提供商(例如 AWS Lambda)不支持符号链接。 此问题的另一种解决方案是在部署之前打包您的应用程序。
    3. 如果你想用 "bundledDependencies" 发布你的包。
    4. 如果您使用 --preserve-symlinks 标志运行 Node.js。
  • pnp - 没有 node_modules。 Plug'n'Play 是一种 Yarn Berry 使用的创新的 Node 依赖策略。 当使用 pnp 作为您的链接器时,建议还将 symlink 设置为 false

添加于:v5.9.0

  • 默认值: true
  • 类型:Boolean

symlink 设置为 false 时,pnpm 创建一个没有任何符号链接的虚拟存储目录。 与 node-linker=pnp 一起是一个有用的设置。

enable-modules-dir

添加于:v5.15.0

  • 默认值: false
  • 类型:Boolean

false 时,pnpm 不会将任何文件写入模块目录(node_modules)。 这对于在用户空间的文件系统 (FUSE) 中挂载模块目录时很有用。 有一个实验性 CLI 允许您在 FUSE 中挂载模块目录:@pnpm/mount-modules

virtual-store-dir

添加于:v4.1.0

  • 默认值:node_modules/.pnpm
  • 类型:path

带有指向存储的链接的目录。 所有直接和间接依赖项都链接到此目录中。

这是一个有用的设置,可以解决 Windows 上长路径的问题。 如果您有一些路径很长的依赖项,您可以选择将虚拟存储放在驱动器的根目录中(例如 C:\my-project-store)。

或者您可以将虚拟存储设置为 .pnpm 并将其添加到 .gitignore。 这将使堆栈跟踪更清晰,因为依赖项的路径将会提高一个目录层级。

注意: 虚拟存储不能在多个项目之间共享。 每个项目都应该有自己的虚拟存储(除了在工作空间中被共享的根目录)。

package-import-method

添加于: v1.25.0

  • 默认值:auto
  • 类型:auto, hardlink, copy, clone

控制从存储导入包的方式。

  • auto - 尝试从存储克隆包。 如果不支持克隆则从存储硬链接包。 如果克隆和链接都不支持,则回退到复制
  • hardlink - 从存储硬链接包
  • copy - 从存储复制包
  • clone - 从商店克隆(也称为 copy-on-write 或参考链接)包

modules-cache-max-age

添加于:v6.0.0

  • 默认值: 10080 (以分钟为单位的 7 天)
  • 类型:number

孤立包应该从模块目录中被删除的时间(以分钟为单位)。 pnpm 在模块目录中保存了一个包的缓存。 切换分支或降级依赖项时,这会提高安装速度。

Lockfile 设置

lockfile

Added in: v1.32.0 as shrinkwrap

  • 默认值: true
  • 类型:Boolean

When set to false, pnpm won't read or generate a pnpm-lock.yaml file.

prefer-frozen-lockfile

Added in: v1.37.1 as prefer-frozen-shrinkwrap

  • Default: true (from v1.38.0)
  • 类型:Boolean

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.

注册源 & 身份验证设置

registry

The base URL of the npm package registry (trailing slash included).

<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 

You may also use an environment variable. 例如:

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

<URL>:tokenHelper

添加于:v6.25.0

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.

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

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

tokenHelper=/home/ivan/token-generator

Setting a token helper for the specified registry:

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

<URL>:always-auth

  • 默认值: false
  • 类型:Boolean

Force pnpm to always require authentication (even for GET requests) when accessing the specified registry. 例如:

@babel:registry=https://gitlab.com/api/v4/packages/npm/
//gitlab.com/api/v4/packages/npm/:always-auth=true

registry=https://registry.npmjs.org/
//registry.npmjs.org/:always-auth=true

请求设置

ca

  • Default: The npm CA certificate
  • Type: String, Array or null

The Certificate Authority signing certificate that is trusted for SSL connections to the registry. Values should be in PEM format (AKA "Base-64 encoded X.509 (.CER)"). 例如:

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

Set to null to only allow known registrars, or to a specific CA cert to trust only that specific signing authority.

Multiple CAs can be trusted by specifying an array of certificates:

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

See also the strict-ssl config.

cafile

  • 默认值:null
  • 类型:path

A path to a file containing one or multiple Certificate Authority signing certificates. 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.

cert

  • 默认值:null
  • Type: String

A client certificate to pass when accessing the registry. Values should be in PEM format (AKA "Base-64 encoded X.509 (.CER)"). 例如:

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

It is not the path to a certificate file (and there is no certfile option).

https-proxy

  • 默认值:null
  • Type: url

A proxy to use for outgoing HTTPS requests. If the HTTPS_PROXY, https_proxy, HTTP_PROXY or http_proxy environment variables are set, their values will be used instead.

key

  • 默认值:null
  • Type: String

A client key to pass when accessing the registry. Values should be in PEM format (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).

local-address

  • 默认值:undefined
  • Type: IP Address

The IP address of the local interface to use when making connections to the npm registry.

proxy

  • 默认值:null
  • Type: url

A proxy to use for outgoing http requests. If the HTTP_PROXY or http_proxy environment variables are set, proxy settings will be honored by the underlying request library.

maxsockets

添加于:v6.18.0

  • 默认值:network-concurrency x 3
  • 类型:Number

每个源使用的最大连接数(协议/主机/端口组合)。

noproxy

Added in: v5.18.8

  • 默认值:null
  • Type: String

A comma-separated string of domain extensions that a proxy should not be used for.

strict-ssl

  • 默认值: true
  • 类型:Boolean

Whether or not to do SSL key validation when making requests to the registry via HTTPS.

See also the ca option.

network-concurrency

  • Default: 16
  • 类型:Number

Controls the maximum number of HTTP(S) requests to process simultaneously.

fetch-retries

  • Default: 2
  • 类型:Number

How many times to retry if pnpm fails to fetch from the registry.

fetch-retry-factor

  • Default: 10
  • 类型:Number

The exponential factor for retry backoff.

fetch-retry-mintimeout

  • Default: 10000 (10 seconds)
  • 类型:Number

The minimum (base) timeout for retrying requests.

fetch-retry-maxtimeout

  • Default: 60000 (1 minute)
  • 类型:Number

The maximum fallback timeout to ensure the retry factor does not make requests too long.

fetch-timeout

添加于:v6.2.0

  • Default: 60000 (1 minute)
  • 类型:Number

等待 HTTP 请求完成的最长时间。

Peer Dependency Settings

auto-install-peers

添加于:v6.26.0

  • 默认值: false
  • 类型:Boolean

When true, pnpm add <pkg> automatically installs any missing peer dependencies as dev dependencies.

strict-peer-dependencies

添加于:v2.15.0

  • 默认值: false
  • 类型:Boolean

If this is enabled, commands will fail if there is a missing or invalid peer dependency in the tree.

命令行设置

[no-]color

添加于:v4.1.0

  • 默认值:auto
  • Type: auto, always, never

Controls colors in the output.

  • auto - output uses colors when the standard output is a terminal or TTY.
  • always - ignore the difference between terminals and pipes. You’ll rarely want this; in most scenarios, if you want color codes in your redirected output, you can instead pass a --color flag to the pnpm command to force it to use color codes. The default setting is almost always what you’ll want.
  • never - turns off colors. This is the setting used by --no-color.

loglevel

Added in: v4.13.0

  • Default: info
  • Type: debug, info, warn, error

Any logs at or higher than the given level will be shown. You can instead pass --silent to turn off all output logs.

use-beta-cli

Added in: v3.6.0

  • 默认值: false
  • 类型:Boolean

Experimental option that enables beta features of the CLI. This means that you may get some changes to the CLI functionality that are breaking changes, or potentially bugs.

recursive-install

Added in: v5.4.0

  • 默认值: true
  • 类型:Boolean

If this is enabled, the primary behaviour of pnpm install becomes that of pnpm install -r, meaning the install is performed on all workspace or subdirectory packages.

Else, pnpm install will exclusively build the package in the current directory.

engine-strict

  • 默认值: false
  • 类型:Boolean

If this is enabled, pnpm will not install any package that claims to not be compatible with the current Node version.

Regardless of this configuration, installation will always fail if a project (not a dependency) specifies an incompatible version in its engines field.

npm-path

添加于:v4.8.0

  • 类型:path

The location of the npm binary that pnpm uses for some actions, like publishing.

构建设置

child-concurrency

  • Default: 5
  • 类型:Number

The maximum number of child processes to allocate simultaneously to build node_modules.

side-effects-cache

Added in: v1.31.0

  • 默认值: false
  • 类型:Boolean
  • Stability: Experimental

Use and cache the results of (pre/post)install hooks.

side-effects-cache-readonly

Added in: v1.31.0

  • 默认值: false
  • 类型:Boolean
  • Stability: Experimental

Only use the side effects cache if present, do not create it for new packages.

unsafe-perm

  • Default: false IF running as root, ELSE true
  • 类型:Boolean

Set to true to enable UID/GID switching when running package scripts. If set explicitly to false, then installing as a non-root user will fail.

Node.js 设置

use-node-version

添加于:v6.5.0

  • 默认值:undefined
  • 类型:semver

指定应用于项目运行时的确切 Node.js 版本。 pnpm 将自动安装指定版本的 Node.js 并将其用于执行 pnpm run 命令或 pnpm node 命令。

node-mirror:<releaseDir>

添加于:v6.24.0

  • 默认值: https://nodejs.org/download/<releaseDir>/
  • 类型:URL

设置用于下载 Node.js 的基本 URL。 此设置的 <releaseDir> 部分可以是 https://nodejs.org/download: release, rc, nightly, v8-canary 等中的任何目录。

以下是如何配置 pnpm 从中国的 Node.js 镜像下载 Node.js:

node-mirror:release=https://npmmirror.com/mirrors/node/
node-mirror:rc=https://npmmirror.com/mirrors/node-rc/
node-mirror:nightly=https://npmmirror.com/mirrors/node-nightly/

其它设置

use-running-store-server

Added in: v2.5.0

  • 默认值: false
  • 类型:Boolean

Only allows installation with a store server. If no store server is running, installation will fail.

save-prefix

  • Default: '^'
  • Type: String

Configure how versions of packages installed to a package.json file get prefixed.

For example, if a package has version 1.2.3, by default its version is set to ^1.2.3 which allows minor upgrades for that package, but after pnpm config set save-prefix='~' it would be set to ~1.2.3 which only allows patch upgrades.

This setting is ignored when the added package has a range specified. For instance, pnpm add foo@2 will set the version of foo in package.json to 2, regardless of the value of save-prefix.

tag

  • Default: latest
  • Type: String

If you pnpm add a package and you don't provide a specific version, then it will install the package at the version registered under the tag from this setting.

This also sets the tag that is added to the package@version specified by the pnpm tag command if no explicit tag is given.

global-dir

Added in: v4.2.0

  • Default: <path to node>/pnpm-global
  • 类型:path

Specify a custom directory to store global packages.

global-bin-dir

添加于:v6.15.0

允许设置全局安装包的 bin 文件的目标目录。

state-dir

添加于:v6.10.0

  • 默认值:$XDG_STATE_HOME/pnpm
  • 类型:path

pnpm 创建的当前仅由更新检查器使用的 pnpm-state.json 文件的目录。

cache-dir

添加于:v6.10.0

  • 默认值:$XDG_CACHE_HOME/pnpm
  • 类型:path

包元数据缓存的位置。

use-stderr

添加于:v6.5.0

  • 默认值: false
  • 类型:Boolean

当为 true 时,所有输出都写入 stderr。

extend-node-path

添加于:v6.16.0

  • 默认值: true
  • 类型:Boolean

false时,命令 shims 中不会设置 NODE_PATH 环境变量。 如果在运行命令时遇到如下错误,建议将此设置设置为 false

The input line is too long. The syntax of the command is incorrect.