跳至主要內容
版本:下一個

pnpm run

指令別名:run-script

Runs a script defined in the package's manifest file.

Examples

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! Simple, right? 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).

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.

Options

run 命令的任何選項都應列在指令碼名稱之前。 列在指令碼之後的選項將傳遞至已執行的指令碼。

所有這些內容都將以 --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

用於指令碼的命令介面以 pnpm run 執行。

例如,要在 Windows 上強制使用 Git Bash:

pnpm config set script-shell "C:\\Program Files\\git\\bin\\bash.exe"

shell-emulator

  • 預設值:false
  • 類型:Boolean

若為 true,pnpm 將使用 bash-like shell 的 JavaScript 執行方式執行指令碼。

此選項簡化了跨平台指令碼處理作業。 例如,預設狀況下,下一個指令碼將在不與 POSIC 相容的系統上失敗:

"scripts": {
"test": "NODE_ENV=test node test.js"
}

但是如果 shell-emulator 設定設為 true,此指令碼將在所有平台上都適用。

--recursive, -r

這會從每個套件的 "scripts" 物件中執行任意一個命令。 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

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

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.

--filter <package_selector>

Read more about filtering.