pnpm config

别名:c

管理配置文件。

pnpm 设置分为两种类型的配置文件:

  • Registry and authentication settings live in INI files: the global rc file, auth.ini, and local .npmrc files.
  • All other pnpm settings live in YAML files: the global config.yaml and the per-project pnpm-workspace.yaml.

本地工作区配置文件位于项目根目录,名为 pnpm-workspace.yaml。全局 YAML 配置文件(config.yaml)位于:

  • 如果设置了 $XDG_CONFIG_HOME 环境变量,则 $XDG_CONFIG_HOME/pnpm/config.yaml
  • 在 Windows 上:~/AppData/Local/pnpm/config/config.yaml
  • 在 macOS 上:~/Library/Preferences/pnpm/config.yaml
  • 在 Linux 上:~/.config/pnpm/config.yaml

全局 rc 文件(仅包含注册表/身份验证设置)位于:

  • 如果设置了 $XDG_CONFIG_HOME 环境变量,则为 $XDG_CONFIG_HOME/pnpm/rc
  • 在 Windows上:~/AppData/Local/pnpm/config/rc
  • 在 macOS 上:~/Library/preferences/pnpm/rc
  • 在 Linux上:~/.config/pnpm/rc

你还可以通过运行以下命令来获取全局配置文件的路径(v10.21.0 版本新增):

pnpm config get globalconfig

命令#

set <key> <value>#

将配置键设置为提供的值。

如果没有 --json 标志,它会将值解析为纯字符串:

pnpm config set --location=project nodeVersion 22.0.0

使用 --json 标志,它将值解析为 JSON:

pnpm config set --location=project --json nodeVersion '"22.0.0"'

--json 标志还允许 pnpm config set 创建数组和对象:

pnpm config set --location=project --json allowBuilds '{"react": true, "react-dom": true}'
pnpm config set --location=project --json catalog '{ "react": "19" }'

set 命令不接受属性路径。

Since v11.22.0, pnpm config set refuses to write a setting to a project's pnpm-workspace.yaml that pnpm does not read from there, such as configDir, pnpmHomeDir, stateDir, and the other settings that name machine-level state. Since v11.25.0, this includes scope, which controls the machine-wide default for pnpm login. 该命令会以 ERR_PNPM_CONFIG_SET_NOT_A_PROJECT_SETTING 失败,并在该设置应有归属时指出它应放在哪里。 pnpm config delete 仍可从已包含该键的文件中清除该键。

get <key>#

打印所提供键的配置值。

key 可以是一个简单的键:

pnpm config get nodeVersion
pnpm config get --json nodeVersion
pnpm config get --json packageExtensions
pnpm config get --json allowBuilds
pnpm config get --json catalog

它也可以是一个属性路径:

pnpm config get 'packageExtensions["@babel/parser"].peerDependencies["@babel/types"]'
pnpm config get --json 'packageExtensions["@babel/parser"].peerDependencies["@babel/types"]'
pnpm config get 'allowBuilds.react'
pnpm config get --json 'allowBuilds.react'
pnpm config get catalog.react
pnpm config get --json catalog.react

属性路径的语法模拟 JavaScript 属性路径。

delete <key>#

从配置文件中删除配置键。

list#

显示所有配置设置。输出结果为 JSON 对象。

与身份验证相关的设置不会显示在输出中;使用 pnpm config get <key> 可以显式地读取它们。

注意

自 v11 版本起,pnpm config get(不带 --json 参数)不再打印 INI 格式的文本。它对对象和数组打印 JSON 格式,对字符串、数字、布尔值和空值打印原始字符串格式。 pnpm config get --json 将所有值打印为 JSON 格式。 pnpm config list 总是打印一个 JSON 对象。

配置项#

--global, -g#

在全局配置文件中设置配置项。

With get and list, show the global configuration without the settings from the project's pnpm-workspace.yaml and .npmrc.

--location#

默认情况下,--location 设置为 global。

当设置为 project 时,pnpm 会将设置写入工作区根目录下的 pnpm-workspace.yaml 文件(或者,对于注册源/身份验证设置,写入工作区根目录下的 .npmrc 文件)。

当设置为 global 时,其行为与传递 --global 选项相同。

--json#

让 get 和 list 以JSON 格式显示所有配置设置,并将 set 解析为 JSON 值。