脚本
pnpm 如何处理 package.json 的 scripts 字段。
隐藏脚本#
名称以 . 开头的脚本将被隐藏。它们不能通过 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
The pnpm npm package provides bundled node-gyp to lifecycle scripts even when pnpm's own install scripts were skipped.
注意
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 :_).
Build options for native addons#
pnpm install does not turn extra command-line flags into npm_config_*
variables the way npm install --build-from-source does. Set the variables
in the environment instead. Native addons that use node-pre-gyp or
prebuild-install, such as sqlite3, read them. For example, to build
sqlite3 from source for Electron, approve its build in
allowBuilds, then put the variables in an
env file:
npm_config_build_from_source=true
npm_config_runtime=electron
npm_config_target=38.0.0
npm_config_disturl=https://electronjs.org/headers
Then load it with --env-file:
pnpm --env-file .env.electron install
To set them for every install of a project without a flag, return them in
extraEnv from the
updateConfig
hook:
export const hooks = {
updateConfig (config) {
return {
...config,
extraEnv: {
...config.extraEnv,
npm_config_build_from_source: 'true',
npm_config_runtime: 'electron',
npm_config_target: '38.0.0',
npm_config_disturl: 'https://electronjs.org/headers',
},
}
},
}
The side-effects cache does not include
these variables in its key, so pnpm can restore a build made with different
values. List the package in
sideEffectsCache.exclude so
pnpm never restores its build from the cache:
sideEffectsCache:
exclude:
- sqlite3
A repeat install does not rebuild a package that is already installed. After changing the variables, rebuild the package in every project with them:
pnpm --env-file .env.electron pm rebuild -r sqlite3
With the updateConfig hook, drop --env-file .env.electron.
pnpm pm runs the built-in command even if package.json
defines a rebuild script.
处理脚本遗留的运行中进程#
A process that a script or a command run by pnpm exec or pnpm dlx starts
and leaves running, such as a daemon, an editor, or a browser opened by the
open package, keeps running after pnpm exits. It does so whether the script
succeeded or failed, and after Ctrl+C, just as it would if the script ran
without pnpm. 关闭终端会结束附加到它的进程,正如操作系统一贯所做的那样。
pnpm 仅在两种情况下会结束它启动的进程:
- When pnpm itself is killed or crashes while a script runs, for example when
a CI job is cancelled. On Windows this ends everything the script started,
except a process started with
CREATE_BREAKAWAY_FROM_JOB. On Unix it ends the scripts that run in a process group of their own, which is every script of a parallel run and every script started without a terminal. A process that leaves its process group, such as a daemon, keeps running. - When a script fails in a recursive or parallel run, pnpm stops the scripts
of the other packages that are still running, unless
--no-bailis set.
内置命令与脚本名称冲突#
The following built-in commands prefer user scripts: clean (and its alias purge), setup, deploy, rebuild (and its alias rb), and tasks. 如果你的 package.json 定义了一个具有这些名称之一的脚本,pnpm <name> 将执行该脚本而不是内置命令。
要强制使用内置命令,请使用 pnpm pm <name>。
生命周期脚本#
pnpm install runs these scripts of each project in the workspace, in order:
preinstall, install, postinstall, preprepare, prepare, and
postprepare. With --prod, or when it installs specific packages, it runs
only preinstall, install, and postinstall.
For pre and post scripts of other scripts run by pnpm run,
see enablePrePostScripts.
pnpm:devPreinstall#
Runs when pnpm install runs in the project itself, including in CI. 当该包作为另一个项目的依赖安装时,它不会运行。
在安装任何依赖项之前运行。
此脚本仅设置在根项目的 package.json 时才执行。