Skip to main content
Version: 11.x

脚本

pnpm 如何处理 package.jsonscripts 字段。

🌐 How pnpm handles the scripts field of package.json.

隐藏脚本

🌐 Hidden Scripts

新增于:v11.0.0

🌐 Added in: v11.0.0

. 开头命名的脚本是隐藏的。它们不能通过 pnpm run 直接运行,并且会从 pnpm run 列表中省略。隐藏脚本只能从其他脚本中调用。

🌐 Scripts with names starting with . are hidden. They cannot be run directly via pnpm run and are omitted from the pnpm run listing. Hidden scripts can only be called from other scripts.

{
"scripts": {
".helper": "echo 'I am hidden'",
"build": "pnpm run .helper && tsc"
}
}

在这个例子中,pnpm run .helper 会失败,但 pnpm run build 会成功,因为 .helper 是从另一个脚本调用的。

🌐 In this example, pnpm run .helper would fail, but pnpm run build would succeed because .helper is called from another script.

环境变量

🌐 Environment Variables

pnpm 在生命周期脚本执行期间设置以下环境变量:

🌐 pnpm sets the following environment variables during lifecycle script execution:

  • npm_package_name — the package name

  • npm_package_version — the package version

  • npm_lifecycle_event — the name of the running script (e.g., postinstall)

note

从 v11 开始,pnpm 不再从 pnpm 配置中填充 npm_config_* 环境变量。只有上面提到的众所周知的 npm_* 变量会被设置,这与 Yarn 的行为一致。

🌐 Since v11, pnpm no longer populates npm_config_* environment variables from the pnpm configuration. Only the well-known npm_* variables above are set, matching Yarn's behavior.

内置命令和脚本名称冲突

🌐 Built-in Command and Script Name Conflicts

新增于:v11.0.0

🌐 Added in: v11.0.0

以下内置命令更倾向于用户脚本:cleansetupdeployrebuild。如果你的 package.json 定义了一个与这些名称相同的脚本,pnpm <name> 将执行该脚本,而不是内置命令。

🌐 The following built-in commands prefer user scripts: clean, setup, deploy, and rebuild. If your package.json defines a script with one of these names, pnpm <name> will execute the script instead of the built-in command.

要强制使用内置命令,请使用 pnpm pm <name>

🌐 To force the built-in command, use pnpm pm <name>.

生命周期脚本

🌐 Lifecycle Scripts

pnpm:devPreinstall

仅在本地 pnpm install 上运行。

🌐 Runs only on local pnpm install.

在安装任何依赖之前运行。

🌐 Runs before any dependency is installed.

只有在根项目的 package.json 中设置时,此脚本才会执行。

🌐 This script is executed only when set in the root project's package.json.