跳到主内容
版本:11.x

脚本

pnpm 如何处理 package.jsonscripts 字段。

隐藏脚本

添加于:v11.0.0

Scripts with names starting with . are hidden. They cannot be run directly via pnpm run and are omitted from the pnpm run listing. 隐藏脚本只能从其他脚本调用。

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

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

环境变量

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

  • npm_package_name — the package name
  • npm_package_version — the package version
  • npm_lifecycle_event — the name of the running script (e.g., postinstall)

内置命令和脚本名称冲突

添加于:v11.0.0

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.

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

生命周期脚本

pnpm:devPreinstall

仅在本地 pnpm install 时运行。

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

此脚本仅设置在根项目的 package.json 时才执行。