pnpm run
Aliases: 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).
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 with 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 - 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 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