pnpm run
Aliases: run-script
运行一个在 package
的 manifest 文件中定义的脚本。
示例
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
! 很简单吧?
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).
运行多个脚本
你可以使用正则表达式来替代脚本名称从而同时运行多个脚本。
pnpm run "/<regex>/"
Run all scripts that start with watch:
:
pnpm run "/^watch:.*/"
详细说明
In addition to the shell’s pre-existing PATH
, pnpm run
includes
node_modules/.bin
in the PATH
provided to scripts
. 这意味着,只要你安装了一个包,你就可以像常规命令一样在脚本中使用它。 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
.
运行环境
pnpm 会自动为执行的脚本创建一些环境变量。 这些环境变量可用于获取有关正在运行的进程的上下文信息。
以下是 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".
配置项
Any options for the run
command should be listed before the script's name.
脚本名称后列出的options将传递给执行的脚本。
All these will run pnpm CLI with the --silent
option:
pnpm run --silent watch
pnpm --silent run watch
pnpm --silent watch
脚本名称后的任何参数都将添加到执行的脚本中。
So if watch
runs webpack --watch
, then this command:
pnpm run watch --no-color
将运行:
webpack --watch --no-color