跳到主内容
Version: 5.x

pnpm run

别名: run-script

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

示例

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

"scripts": {
"watch": "build-command --watch"
}

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

详细

除了 shell 先前存在的 PATHpnpm run 也包括在 PATH 中的 node_modules/.bin提供的scripts。 这意味着只要您安装了一个 package,您就可以像普通的命令一样在脚本中使用。 例如,如果您安装了 eslint ,您可以像这样写一个脚本:

"lint": "eslint src --fix"

甚至如果 eslint 没有在你的 shell 中全局安装,它也会运行。

对于工作空间,截至 v3.5 , <workspace root>/node_modules/.bin 也将添加到PATH中,因此如果在工作空间根目录中安装了一个工具,则可以在工作空间的任何packagescripts中使用。

配置项

script-shell

Added in: v5.10.0

  • 默认值: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 配置中设置 script-shell "C:\\Program Files\\git\\bin\\bash.exe"

shell-emulator

Added in: v5.8.0

  • 默认值: 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

Added in: v4.5.0

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

添加于:v5.1.0

Completely disregard concurrency and topological sorting, running a given script immediately in all matching packages with prefixed streaming output. This is the preferred flag for long-running processes over many packages, for instance, a lengthy build process.

--stream

添加于:v5.1.0

Stream output from child processes immediately, prefixed with the originating package directory. This allows output from different packages to be interleaved.

--filter <package_selector>

阅读更多有关 filter 的内容。