pnpm run
Aliases:run-script
Runs a script defined in the package's manifest file.
Examples
Let's say you have awatch
script configured in yourpackage.json
, like so:
"scripts":{
"watch":"webpack --watch"
}
You can now run that script by usingpnpm run watch
! Simple, right?Another thing to note for those that like to save keystrokes and time is thatall scripts get aliased in as pnpm commands, so ultimatelypnpm watch
is justshorthand forpnpm run watch
(ONLY for scripts that do not share the same nameas already existing pnpm commands).
Running multiple scripts
You may run multiple scripts at the same time by using a regex instead of the script name.
pnpm run "/<regex>/"
Run all scripts that start withwatch:
:
pnpm run "/^watch:.*/"
Details
In addition to the shell’s pre-existingPATH
,pnpm run
includesnode_modules/.bin
in thePATH
provided toscripts
. This means that solong as you have a package installed, you can use it in a script like a regularcommand. For example, if you haveeslint
installed, you can write up a scriptlike so:
"lint":"eslint src --fix"
And even thougheslint
is not installed globally in your shell, it will run.
For workspaces,<workspace root>/node_modules/.bin
is also addedto thePATH
, so if a tool is installed in the workspace root, it may be calledin any workspace package'sscripts
.
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 - contains the name of the executed command. If the executed command is
pnpm run
, then the value of this variable will be "run-script".
Options
Any options for therun
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 ifwatch
runswebpack --watch
, then this command:
pnpm run watch --no-color
will run:
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 codewhen the script is undefined. This lets you run potentially undefined scriptswithout breaking the execution chain.
--parallel
Completely disregard concurrency and topological sorting, running a given scriptimmediately in all matching packages with prefixed streaming output. This is thepreferred flag for long-running processes over many packages, for instance, alengthy build process.
--stream
Stream output from child processes immediately, prefixed with the originatingpackage 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 runningpnpm -r <command>
with--parallel
or with--workspace-concurrency=<number>
much easier (especially on CI). Only--reporter=append-only
is supported.
--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 apnpm-exec-summary.json
file.
An example of apnpm-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 ofstatus
are: 'passed', 'queued', 'running'.
--reporter-hide-prefix
Hide workspace prefix from output from child processes that are run in parallel, and only print the raw output. 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 settings
enable-pre-post-scripts
- Default:true
- 型別:布林值
Whentrue
, pnpm will run any pre/post scripts automatically. So runningpnpm foo
will be like runningpnpm prefoo && pnpm foo && pnpm postfoo
.
script-shell
- Default:null
- Type:path
The shell to use for scripts run with thepnpm 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
- 型別:布林值
Whentrue
, pnpm will use a JavaScript implementation of abash-like shell toexecute scripts.
This option simplifies cross-platform scripting. For instance, by default, thenext script will fail on non-POSIX-compliant systems:
"scripts":{
"test":"NODE_ENV=test node test.js"
}
But if theshell-emulator
setting is set totrue
, it will work on allplatforms.