脚本
pnpm 如何处理 package.json 的 scripts 字段。
Hidden scripts#
添加于:v11.0.0
名称以 . 开头的脚本将被隐藏。它们不能通过 pnpm run 直接运行,并且会从 pnpm run 列表中省略。隐藏脚本只能从其他脚本调用。
{
"scripts": {
".helper": "echo 'I am hidden'",
"build": "pnpm run .helper && tsc"
}
}
在这个例子中,pnpm run .helper 会失败,但 pnpm run build 会成功,因为 .helper 是从另一个脚本调用的。
环境变量#
pnpm 在生命周期脚本执行期间设置以下环境变量:
npm_package_name: the package namenpm_package_version: the package versionnpm_package_config_*,npm_package_engines_*,npm_package_bin_*: theconfig,engines, andbinfields of the manifest, one variable per entry (for example,npm_package_config_port). A stringbinsetsnpm_package_bin.npm_package_json: the path to the package'spackage.jsonnpm_lifecycle_event: the name of the running script (e.g.,postinstall)npm_lifecycle_script: the command of the running scriptnpm_node_execpathandNODE: the path to the Node.js executablenpm_execpath: the path to the running pnpmnpm_config_user_agent: pnpm's user agent stringnpm_config_node_gyp: the path to the bundlednode-gyp, unless the environment already sets itINIT_CWD: the directory where pnpm was startedPNPM_SCRIPT_SRC_DIR: the directory of the package whose script runsTMPDIR:node_modules/.tmpin the package, unlessunsafePermistrue
注意
Apart from the variables above, pnpm does not set npm_config_* variables from
its configuration. Other npm_config_* variables in the environment reach the
script unchanged, except auth, scope-registry, and per-registry settings (names
starting with _, /, or @, or containing :_).
Built-in command and script name conflicts#
添加于:v11.0.0
以下内置命令优先使用用户脚本:clean、setup、deploy 和 rebuild。如果你的 package.json 定义了一个具有这些名称之一的脚本,pnpm <name> 将执行该脚本而不是内置命令。
要强制使用内置命令,请使用 pnpm pm <name>。
Lifecycle scripts#
pnpm:devPreinstall#
Runs when pnpm install runs in the project itself, including in CI. It does not run when the package is installed as a dependency of another project.
在安装任何依赖项之前运行。
此脚本仅设置在根项目的 package.json 时才执行。
Since v11.28.0, installs that skip devDependencies, such as pnpm install --prod, do not run it.
preinstall of the root project#
Since v11.28.0, the root project's preinstall script runs before dependencies are resolved and linked. A guard such as npx only-allow pnpm can therefore stop the install before pnpm populates node_modules.
prepare#
The root project's prepare script runs after pnpm install. It is skipped, since v11.28.0, by installs that skip devDependencies (such as pnpm install --prod), by pnpm install given package arguments, and for the project that pnpm deploy deploys.
Uninstall scripts#
Added in: v11.28.0
pnpm remove runs the project's own preuninstall, uninstall, and postuninstall scripts. preuninstall and uninstall run before dependencies are unlinked, and a failure in either aborts the removal. postuninstall runs after unlinking completes. The ignoreScripts setting and --lockfile-only skip all three.