npm-run-script
选择命令行版本:
See Details
目录
概要
¥Synopsis
npm run-script <command> [-- <args>]aliases: run, rum, urn
描述
¥Description
这会从包的 "scripts"
对象运行任意命令。如果没有提供 "command"
,它将列出可用的脚本。
¥This runs an arbitrary command from a package's "scripts"
object. If no "command"
is provided, it will list the available scripts.
run[-script]
由 test、start、restart 和 stop 命令使用,但也可以直接调用。当包中的脚本被打印出来时,它们被分为生命周期(测试、启动、重启)和直接运行的脚本。
¥run[-script]
is used by the test, start, restart, and stop commands, but can be called directly, as well. When the scripts in the package are printed out, they're separated into lifecycle (test, start, restart) and directly-run scripts.
任何位置参数都会传递给指定的脚本。使用 --
传递以 -
为前缀的标志和选项,否则它们会被 npm 解析。
¥Any positional arguments are passed to the specified script. Use --
to pass -
-prefixed flags and options which would otherwise be parsed by npm.
例如:
¥For example:
npm run test -- --grep="pattern"
参数只会传递给 npm run
之后指定的脚本,而不是任何 pre
或 post
脚本。
¥The arguments will only be passed to the script specified after npm run
and not to any pre
or post
script.
env
脚本是一个特殊的内置命令,可用于列出脚本在运行时可用的环境变量。如果你的包中定义了 "env" 命令,它将优先于内置命令。
¥The env
script is a special built-in command that can be used to list environment variables that will be available to the script at runtime. If an "env" command is defined in your package, it will take precedence over the built-in.
除了 shell 预先存在的 PATH
之外,npm run
还将 node_modules/.bin
添加到提供给脚本的 PATH
中。本地安装的依赖提供的任何二进制文件都可以在没有 node_modules/.bin
前缀的情况下使用。例如,如果你的包中 tap
上有一个 devDependency
,你应该写:
¥In addition to the shell's pre-existing PATH
, npm run
adds node_modules/.bin
to the PATH
provided to scripts. Any binaries provided by locally-installed dependencies can be used without the node_modules/.bin
prefix. For example, if there is a devDependency
on tap
in your package, you should write:
"scripts": {"test": "tap test/*.js"}
代替
¥instead of
"scripts": {"test": "node_modules/.bin/tap test/*.js"}
运行脚本的实际 shell 取决于平台。默认情况下,在类 Unix 系统上是 /bin/sh
命令,在 Windows 上是 cmd.exe
。/bin/sh
所指的实际外壳也取决于系统。你可以使用 script-shell
配置 自定义外壳。
¥The actual shell your script is run within is platform dependent. By default, on Unix-like systems it is the /bin/sh
command, on Windows it is cmd.exe
. The actual shell referred to by /bin/sh
also depends on the system. You can customize the shell with the script-shell
config.
脚本从包文件夹的根目录运行,无论调用 npm run
时当前工作目录是什么。如果你希望脚本根据你所在的子目录使用不同的行为,你可以使用 INIT_CWD
环境变量,它包含你在运行 npm run
时所在的完整路径。
¥Scripts are run from the root of the package folder, regardless of what the current working directory is when npm run
is called. If you want your script to use different behavior based on what subdirectory you're in, you can use the INIT_CWD
environment variable, which holds the full path you were in when you ran npm run
.
npm run
将 NODE
环境变量设置为执行 npm
的 node
可执行文件。
¥npm run
sets the NODE
environment variable to the node
executable with which npm
is executed.
如果你尝试在没有 node_modules
目录的情况下运行脚本但它失败了,你将收到运行 npm install
的警告,以防你忘记了。
¥If you try to run a script without having a node_modules
directory and it fails, you will be given a warning to run npm install
, just in case you've forgotten.
工作区支持
¥Workspaces support
你可以使用 workspace
或 workspaces
配置,以便在指定工作区的上下文中从包的 "scripts"
对象运行任意命令。如果未提供 "command"
,它将列出每个已配置工作区的可用脚本。
¥You may use the workspace
or workspaces
configs in order to run an arbitrary command from a package's "scripts"
object in the context of the specified workspaces. If no "command"
is provided, it will list the available scripts for each of these configured workspaces.
给定一个具有已配置工作区的项目,例如:
¥Given a project with configured workspaces, e.g:
.+-- package.json`-- packages+-- a| `-- package.json+-- b| `-- package.json`-- c`-- package.json
假设工作区配置在根级别 package.json
文件中正确设置。例如:
¥Assuming the workspace configuration is properly set up at the root level package.json
file. e.g:
{"workspaces": [ "./packages/*" ]}
并且每个已配置的工作区都有一个已配置的 test
脚本,我们可以使用 workspaces
配置 在所有工作区中运行测试:
¥And that each of the configured workspaces has a configured test
script, we can run tests in all of them using the workspaces
config:
npm test --workspaces
过滤工作区
¥Filtering workspaces
也可以使用 workspace
配置以及名称或目录路径在单个工作区中运行脚本:
¥It's also possible to run a script in a single workspace using the workspace
config along with a name or directory path:
npm test --workspace=a