跳到主内容
版本:11 & 12

构建设置

ignoreScripts

  • 默认值: false
  • 类型:Boolean

不执行项目 package.json 及其依赖中定义的任何脚本。

注意

This flag does not prevent the execution of .pnpmfile.mjs

childConcurrency

  • 默认值:5
  • 类型:Number

同时分配的最大子进程数以构建 node_modules。

sideEffectsCache

  • 默认值:true
  • 类型:Boolean

使用且缓存 (pre/post)install 钩子的结果。

当安装前/安装后脚本修改包的内容(例如构建输出)时,pnpm 会将修改后的包保存在全局存储中。 在同一台机器上的未来安装中,pnpm 会重复使用这个缓存的预构建版本,从而使安装速度显著加快。

注意

你可能想要禁用此设置,如果:

  1. 安装脚本修改包目录外部的文件(pnpm 无法跟踪或缓存这些更改)。
  2. 这些脚本执行的副作用与构建包无关。

sideEffectsCacheReadonly

  • 默认值: false
  • 类型:Boolean

仅在存在 side effects cache 时使用,不要为新包创建它。

unsafePerm

  • 默认值:如果以 root 身份运行,则为 false,否则为 true
  • 类型:Boolean

设置为 true 以便在运行包脚本package scripts时启用 UID/GID 切换。 如果显式设置为 false,则以非 root 用户身份安装将失败。

nodeOptions

  • 默认值: NULL
  • 类型:字符串

通过 NODE_OPTIONS 环境变量传递给 Node.js 的选项。 这不会影响 pnpm 本身的执行方式,但会影响生命周期脚本的调用方式。

为了保留现有的 NODE_OPTIONS,你可以在配置中使用 ${NODE_OPTIONS} 引用现有的环境变量:环境

nodeOptions: "${NODE_OPTIONS:- } --experimental-vm-modules"

verifyDepsBeforeRun

  • 默认: install
  • 类型:installwarnerrorpromptfalse

此设置允许在运行脚本之前检查依赖项的状态。 检查运行于 pnpm runpnpm exec 命令。 支持以下属性值:

  • install — 如果 node_modules 不是最新的,则自动运行安装。
  • warn - 如果 node_modules 不是最新的,则打印警告。
  • prompt - 如果 node_modules 不是最新的,则提示用户提供权限。
  • error - 如果 node_modules 不是最新的,则会引发错误。
  • false——禁用依赖性检查。

strictDepBuilds

添加于:v10.3.0

  • 默认值:true
  • 类型:Boolean

当启用 strictDepBuilds 时,如果任何依赖项具有未审核的构建脚本(又名安装后脚本),则安装将以非零退出代码退出。

allowBuilds

添加于:v10.26.0

包匹配器映射,用于显式允许(true)或禁止(false)脚本执行。

allowBuilds:
esbuild: true
core-js: false
# 未在下方列出的 nx 版本构建脚本将
# 在默认情况下失败,并返回 ERR_PNPM_IGNORED_BUILDS 错误
nx@21.6.4 || 21.6.5: true
nx@21.6.0: false

Git-hosted packages: a package name on its own never approves builds for a git or tarball dependency — the name alone does not identify the artifact. Approve one either by its exact resolved path (including the commit) or, since v11.11.0, by its repository URL:

allowBuilds:
# Approves any commit from this repository
'foo@git+ssh://git@example.com/org/foo.git': true
# Approves only this exact commit
'bar@git+https://github.com/org/bar.git#abc123': true

The repository form lets a trusted git dependency keep running its build scripts across branch updates without re-approving each new commit. The key is the package name, followed by @ and the git URL, with no #<ref> suffix. Matching is exact, so git+ssh:// and git+https:// URLs for the same repository are separate keys.

Since v11.19.0, the repository form also approves git-hosted packages that pnpm downloads as a tarball rather than clones — such as github: dependencies, which are fetched from codeload.github.com. A foo@git+https://github.com/org/foo.git entry approves foo whether pnpm clones the repository or downloads a tarball. GitLab and Bitbucket tarball downloads are matched the same way. Approving or denying a specific resolved commit by its full tarball dep path continues to work.

Denials by package name are not restricted this way: foo: false blocks foo whether it comes from the registry or from git.

默认行为: 未在 allowBuilds 中列出的软件包默认情况下是不允许的,并且将被视为未经审核的软件包。 默认情况下会打印错误(strictDepBuilds 默认为 true)。 如果将 strictDepBuilds 设置为 false,则会打印一条警告信息。

安装过程中,尚未在 allowBuilds 中列出的、具有被忽略的构建的依赖项会自动添加到 pnpm-workspace.yaml 中,并带有占位符值,因此您可以手动将其设置为 truefalse。 The --allow-build flag on pnpm add and pnpm approve-builds writes its entries here as well.

从旧设置迁移

To migrate these settings automatically, run pnpx codemod run pnpm-v10-to-v11 from the Migrating from v10 to v11 guide.

v11 中已移除以下设置,并替换为 allowBuildsonlyBuiltDependenciesonlyBuiltDependenciesFileneverBuiltDependenciesignoredBuiltDependenciesignoreDependencies

之前:

onlyBuiltDependencies:
- electron
neverBuiltDependencies:
- core-js
ignoredBuiltDependencies:
- esbuild

之后:

allowBuilds:
electron: true
core-js: false
esbuild: false

dangerouslyAllowAllBuilds

添加于:v10.9.0

  • 默认值: false
  • 类型:Boolean

如果设置为 true,所有从依赖关系生成的脚本(例如preinstall, install)将自动运行,无需批准。

警告

此设置允许所有依赖项(包括传递依赖项)现在和将来运行安装脚本。 即使你当前的依赖图看起来很安全:

  • 未来的更新可能会引入新的、不受信任的依赖项。
  • 现有的软件包可能会在后续版本中添加脚本。
  • 软件包可能会被劫持或破坏并开始执行恶意代码。

为了获得最大程度的安全,只有当你充分了解风险并信任你所依赖的整个生态系统时才启用此功能。 建议明确审查并允许构建。