Skip to main content
Version: Next

pnpm config

别名:c

🌐 Aliases: c

管理配置文件。

🌐 Manage the configuration files.

pnpm 设置分布在两种类型的配置文件中:

🌐 pnpm settings are split across two kinds of configuration files:

  • 注册表和身份验证设置 存储在 INI 文件中——全局 rc 文件和本地 .npmrc 文件。
  • 所有其他 pnpm 设置 都存储在 YAML 文件中——全局的 config.yaml 和每个项目的 pnpm-workspace.yaml

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

🌐 The local workspace configuration file is located at the root of the project and is named pnpm-workspace.yaml. The global YAML configuration file (config.yaml) is located at:

  • 如果设置了 $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 文件(仅限注册表/认证设置)位于:

🌐 The global rc file (registry/auth settings only) is at:

  • 如果设置了 $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 版本新增):

🌐 You can also retrieve the path to your global config file by running (added in v10.21.0):

pnpm config get globalconfig

命令

🌐 Commands

set <key> <value>

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

🌐 Set the config key to the value provided.

没有 --json 标志时,它会将值解析为普通字符串:

🌐 Without the --json flag, it parses the value as plain string:

pnpm config set --location=project nodeVersion 22.0.0

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

🌐 With the --json flag, it parses the value as JSON:

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

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

🌐 The --json flag also allows pnpm config set to create arrays and objects:

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

set 命令不接受属性路径。

🌐 The set command does not accept a property path.

get <key>

打印提供的密钥的配置值。

🌐 Print the config value for the provided key.

key 可以是一个简单的键:

🌐 The key can be a simple 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

它也可以是属性路径:

🌐 It can also be a property path:

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 属性路径。

🌐 The syntax of the property path emulates JavaScript property paths.

delete <key>

从配置文件中删除配置密钥。

🌐 Remove the config key from the config file.

list

显示所有配置设置。输出是一个 JSON 对象。

🌐 Show all the config settings. Output is a JSON object.

与身份验证相关的设置在输出中被隐藏;使用 pnpm config get <key> 来显式读取它们。

🌐 Auth-related settings are hidden from the output; use pnpm config get <key> to read them explicitly.

note

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

🌐 Since v11, pnpm config get (without --json) no longer prints INI-formatted text. It prints JSON for objects and arrays, and raw strings for strings, numbers, booleans, and nulls. pnpm config get --json prints all values as JSON. pnpm config list always prints a JSON object.

选项

🌐 Options

--global, -g

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

🌐 Set the configuration in the global config file.

--location

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

🌐 By default, --location is set to global.

当设置为 project 时,pnpm 会将该设置写入工作区根目录的 pnpm-workspace.yaml(或者,对于注册表/认证设置,写入工作区根目录的 .npmrc)。

🌐 When set to project, pnpm writes the setting to pnpm-workspace.yaml at the workspace root (or, for registry/auth settings, to the .npmrc in the workspace root).

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

🌐 When set to global, the behavior is the same as passing the --global option.

--json

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

🌐 Make get and list show all the config settings in JSON format and make set parse the value as JSON.