pnpm run
别名: run-script
运行一个在 package的 manifest 文件中定义的脚本。
示例
假如您有个 watch 脚本配置在了package.json 中,像这样:
"scripts": {
    "watch": "webpack --watch"
}
您现在可以使用 pnpm run watch运行该脚本! 很简单吧? 对于那些不喜欢敲键盘而浪费时间的人要注意的另一件事是,所有脚本都会有 pnpm 命令的别名,所以最终 pnpm run watch 的简写是 pnpm watch (仅适用于那些不与已有的pnpm 命令相同名字的脚本)。
运行多个脚本
Added in: v7.27.0
你可以使用正则表达式来替代脚本名称从而同时运行多个脚本。
pnpm run "/<regex>/"
运行所有以watch:开头的脚本。
pnpm run "/^watch:.*/"
详细说明
除了在shell中已经存在的 PATH, pnpm run 在脚本中还包含 node_modules/.bin 的 PATH 。 这意味着,只要你安装了一个包,你就可以像常规命令一样在脚本中使用它。 例如,如果你已经安装了 eslint 你可以像这样写一个脚本:
"lint": "eslint src --fix"
尽管 eslint 没有在你的shell中进行全局安装,它也会运行。
对于workspaces, <workspace root>/node_modules/.bin 也会被添加到 到 PATH中,因此如果在工作空间根目录中安装了工具,则可以在任何workspace package的 scripts中调用它 。
与 npm run 的区别
默认情况下, pnpm 不会任意执行用户定义的 hook,pre 和 post (例如 prestart)。 这种从npm继承过来的习惯,会导致脚本执行是隐式的,而不是显式的,从而混淆了执行流程。 它还会导致意外执行 pnpm serve 和 pnpm preserve。
如果出于某种原因,需要 npm 的前置、后置钩子脚本,可使用enable-pre-post-scripts 选项。
运行环境
pnpm 会自动为执行的脚本创建一些环境变量。 这些环境变量可用于获取有关正在运行的进程的上下文信息。
以下是 pnpm 会创建的环境变量:
- npm_command - 包含已执行命令的名称。 如果执行的命令是 
pnpm run,那么这个变量的值就是“run-script”。 
配置项
run 命令的options都应列在脚本名称之前。 脚本名称后列出的options将传递给执行的脚本。
例如下面这些都将使用 --silent 选项运行 pnpm CLI:
pnpm run --silent watch
pnpm --silent run watch
pnpm --silent watch
脚本名称后的任何参数都将添加到执行的脚本中。 所以如果 watch 运行 webpack --watch,那么这个命令:
pnpm run watch --no-color
将运行:
webpack --watch --no-color
script-shell
- 默认值:null
 - 类型: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
- 默认值: false
 - 类型: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.
--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.
enable-pre-post-scripts
- 默认值: false
 - 类型: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.
--resume-from <package_name>
添加于:v7.22.0
从特定项目恢复执行。 如果您正在使用大型工作区,并且想要在不运行先前项目的情况下从特定项目重新启动构建,那么这可能非常有用。
--report-summary
Added in: v7.28.0
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'.