pnpm run
Aliases: run-script
运行一个在 package
的 manifest 文件中定义的脚本。
示例
Let's say you have a watch
script configured in your package.json
, like so:
"scripts": {
"watch": "webpack --watch"
}
You can now run that script by using pnpm run watch
! 很简单吧?
Another thing to note for those that like to save keystrokes and time is that
all scripts get aliased in as pnpm commands, so ultimately pnpm watch
is just
shorthand for pnpm run watch
(ONLY for scripts that do not share the same name
as already existing pnpm commands).
运行多个脚本
你可以使用正则表达式来替代脚本名称从而同时运行多个脚本。
pnpm run "/<regex>/"
Run all scripts that start with watch:
:
pnpm run "/^watch:.*/"
详细说明
In addition to the shell’s pre-existing PATH
, pnpm run
includes
node_modules/.bin
in the PATH
provided to scripts
. 这意味着,只要你安装了一个包,你就可以像常规命令一样在脚本中使用它。 For example, if you have eslint
installed, you can write up a script
like so:
"lint": "eslint src --fix"
And even though eslint
is not installed globally in your shell, it will run.
For workspaces, <workspace root>/node_modules/.bin
is also added
to the PATH
, so if a tool is installed in the workspace root, it may be called
in any workspace package's scripts
.
运行环境
pnpm 会自动为执行的脚本创建一些环境变量。 这些环境变量可用于获取有关正在运行的进程 的上下文信息。
以下是 pnpm 会创建的环境变量:
- npm_command - contains the name of the executed command. If the executed command is
pnpm run
, then the value of this variable will be "run-script".
配置项
Any options for the run
command should be listed before the script's name.
脚本名称后列出的options将传递给执行的脚本。
All these will run pnpm CLI with the --silent
option:
pnpm run --silent watch
pnpm --silent run watch
pnpm --silent watch
脚本名称后的任何参数都将添加到执行的脚本中 。
So if watch
runs webpack --watch
, then this command:
pnpm run watch --no-color
将运行:
webpack --watch --no-color
--recursive, -r
This runs an arbitrary command from each package's "scripts" object. If a package doesn't have the command, it is skipped. If none of the packages have the command, the command fails.
--if-present
You can use the --if-present
flag to avoid exiting with a non-zero exit code
when the script is undefined. This lets you run potentially undefined scripts
without breaking the execution chain.
--parallel
完全忽略并发和拓扑排序,在所有匹配的包中立即运行给定的脚本
并输出前缀流。 这是个推荐的标志,用于在许多 packages
上长时间运行的进程,例如冗长的构建进程。
--stream
Stream output from child processes immediately, prefixed with the originating package directory. This allows output from different packages to be interleaved.
--aggregate-output
Aggregate output from child processes that are run in parallel, and only print output when the child process is finished. It makes reading large logs after running pnpm -r <command>
with --parallel
or with --workspace-concurrency=<number>
much easier (especially on CI). Only --reporter=append-only
is supported.
--resume-from <package_name>
从特定项目恢复执行。 如果您正在使用大型工作区,并且想要在不运行先前项目的情况下从特定项目重新启动构建,那么这可能非常有用。
--report-summary
Record the result of the scripts executions into a pnpm-exec-summary.json
file.
An example of a pnpm-exec-summary.json
file:
{
"executionStatus": {
"/Users/zoltan/src/pnpm/pnpm/cli/command": {
"status": "passed",
"duration": 1861.143042
},
"/Users/zoltan/src/pnpm/pnpm/cli/common-cli-options-help": {
"status": "passed",
"duration": 1865.914958
}
}
Possible values of status
are: 'passed', 'queued', 'running'.
--reporter-hide-prefix
从并行运行的子进程的输出中隐藏工作区前缀,并且仅打印原始输出。 This can be useful if you are running on CI and the output must be in a specific format without any prefixes (e.g. GitHub Actions annotations). Only --reporter=append-only
is supported.
--filter <package_selector>
.npmrc 设置
enable-pre-post-scripts
- Default: true
- Type: Boolean
When true
, pnpm will run any pre/post scripts automatically. So running pnpm foo
will be like running pnpm prefoo && pnpm foo && pnpm postfoo
.
script-shell
- Default: null
- Type: path
The shell to use for scripts run with the pnpm run
command.
For instance, to force usage of Git Bash on Windows:
pnpm config set script-shell "C:\\Program Files\\git\\bin\\bash.exe"
shell-emulator
- Default: false
- Type: Boolean
When true
, pnpm will use a JavaScript implementation of a bash-like shell to
execute scripts.
This option simplifies cross-platform scripting. For instance, by default, the next script will fail on non-POSIX-compliant systems:
"scripts": {
"test": "NODE_ENV=test node test.js"
}
But if the shell-emulator
setting is set to true
, it will work on all
platforms.