跳到主内容
版本:Next

pnpm run

别名: run-script

运行一个在 package的 manifest 文件中定义的脚本。

示例

假如您有个 watch 脚本配置在了package.json 中,像这样:

"scripts": {
"watch": "webpack --watch"
}

您现在可以使用 pnpm run watch运行该脚本! 很简单吧? 对于那些不喜欢敲键盘而浪费时间的人要注意的另一件事是,所有脚本都会有 pnpm 命令的别名,所以最终 pnpm run watch 的简写是 pnpm watch仅适用于那些不与已有的pnpm 命令相同名字的脚本)。

运行多个脚本

你可以使用正则表达式来替代脚本名称从而同时运行多个脚本。

pnpm run "/<regex>/"

运行所有以watch:开头的脚本。

pnpm run "/^watch:.*/"

Details

In addition to the shell’s pre-existing PATH, pnpm run includes node_modules/.bin in the PATH provided to scripts. This means that so long as you have a package installed, you can use it in a script like a regular command. 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.

Differences with npm run

By default, pnpm doesn't run arbitrary pre and post hooks for user-defined scripts (such as prestart). This behavior, inherited from npm, caused scripts to be implicit rather than explicit, obfuscating the execution flow. It also led to surprising executions with pnpm serve also running pnpm preserve.

If for some reason you need the pre/post scripts behavior of npm, use the enable-pre-post-scripts option.

Environment

There are some environment variables that pnpm automatically creates for the executed scripts. These environment variables may be used to get contextual information about the running process.

These are the environment variables created by pnpm:

  • npm_command - 包含已执行命令的名称。 如果执行的命令是 pnpm run,那么这个变量的值就是“run-script”。

配置项

Any options for the run command should be listed before the script's name. Options listed after the script's name are passed to the executed script.

All these will run pnpm CLI with the --silent option:

pnpm run --silent watch
pnpm --silent run watch
pnpm --silent watch

Any arguments after the command's name are added to the executed script. So if watch runs webpack --watch, then this command:

pnpm run watch --no-color

will run:

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>

Resume execution from a particular project. This can be useful if you are working with a large workspace and you want to restart a build at a particular project without running through all of the projects that precede it in the build order.

--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'.

--filter <package_selector>

阅读更多有关 filter 的内容。