npm-run-script

运行任意包脚本

选择命令行版本:

概要

🌐 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 命令使用,但也可以直接调用。当打印包中的脚本时,它们会被分为生命周期脚本(test、start、restart)和可直接运行的脚本。

任何位置参数都会传递给指定的脚本。使用 -- 来传递以 - 为前缀的标志和选项,这些标志和选项否则会被 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 后指定的脚本,而不会传递给任何 prepost 脚本。

🌐 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 预先存在的 PATHnpm run 还向脚本提供的 PATH 添加了 node_modules/.bin。本地安装的依赖提供的任何二进制文件都可以在不加 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 所指的实际 shell 也取决于系统。你可以通过 script-shell 配置 自定义 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 runNODE 环境变量设置为 node 可执行文件,用于执行 npm

如果你尝试运行脚本而没有 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

你可以使用 workspaceworkspaces 配置,从指定工作区的包的 "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

workspace 配置也可以多次指定,以便在多个工作区的上下文中运行特定脚本。在命令行中为 workspace 配置定义值时,也可以使用 -w 作为简写,例如:

🌐 The workspace config can also be specified multiple times in order to run a specific script in the context of multiple workspaces. When defining values for the workspace config in the command line, it also possible to use -w as a shorthand, e.g:

npm test -w a -w b

最后一个命令将在 ./packages/a./packages/b 包中运行 test

🌐 This last command will run test in both ./packages/a and ./packages/b packages.

配置

🌐 Configuration

workspace

  • 默认值:
  • 类型:字符串(可以多次设置)

启用在当前项目的已配置工作区的上下文中运行命令,同时通过仅运行此配置选项定义的工作区进行过滤。

🌐 Enable running a command in the context of the configured workspaces of the current project while filtering by running only the workspaces defined by this configuration option.

workspace 配置的有效值为以下之一:

🌐 Valid values for the workspace config are either:

  • 工作区名称
  • 工作区目录的路径
  • 父工作区目录的路径(将导致选择该文件夹中的所有工作区)

对于 npm init 命令设置时,可以将其设置为一个尚不存在的工作区文件夹,以创建该文件夹并将其作为项目内全新的工作区进行设置。

🌐 When set for the npm init command, this may be set to the folder of a workspace which does not yet exist, to create the folder and set it up as a brand new workspace within the project.

此值不会导出到子进程的环境中。

🌐 This value is not exported to the environment for child processes.

workspaces

  • 默认值:空
  • 类型:空或布尔值

设置为 true 以在 所有 配置的工作区上下文中运行命令。

🌐 Set to true to run the command in the context of all configured workspaces.

将此显式设置为 false 会导致像 install 这样的命令完全忽略工作区。如果不显式设置:

🌐 Explicitly setting this to false will cause commands like install to ignore workspaces altogether. When not set explicitly:

  • node_modules 树操作的命令(安装、更新等)会将工作区链接到 node_modules 文件夹。- 执行其他操作的命令(测试、执行、发布等)会在根项目上运行,除非workspace 配置中指定了一个或多个工作区。

此值不会导出到子进程的环境中。

🌐 This value is not exported to the environment for child processes.

include-workspace-root

  • 默认:否
  • 类型:布尔

为命令启用工作区时包括工作区根。

🌐 Include the workspace root when workspaces are enabled for a command.

当为 false 时,通过 workspace 配置指定单个工作区,或通过 workspaces 标志指定所有工作区,将导致 npm 仅在指定的工作区上操作,而不会在根项目上操作。

🌐 When false, specifying individual workspaces via the workspace config, or all workspaces via the workspaces flag, will cause npm to operate only on the specified workspaces, and not on the root project.

此值不会导出到子进程的环境中。

🌐 This value is not exported to the environment for child processes.

if-present

  • 默认:否
  • 类型:布尔

如果为真,当执行 run-script 用于 package.jsonscripts 部分中未定义的脚本时,npm 不会以错误码退出。此选项可用于在脚本存在时选择性地运行该脚本,并在脚本执行失败时报错。这在某些情况下非常有用,例如在通常通用的 CI 设置中运行可能只适用于某些构建的脚本时。

🌐 If true, npm will not exit with an error code when run-script is invoked for a script that isn't defined in the scripts section of package.json. This option can be used when it's desirable to optionally run a script when it's present and fail if the script fails. This is useful, for example, when running scripts that may only apply for some builds in an otherwise generic CI setup.

此值不会导出到子进程的环境中。

🌐 This value is not exported to the environment for child processes.

ignore-scripts

  • 默认:否
  • 类型:布尔

如果为 true,npm 不会运行 package.json 文件中指定的脚本。

🌐 If true, npm does not run scripts specified in package.json files.

请注意,明确用于运行特定脚本的命令,例如 npm startnpm stopnpm restartnpm testnpm run-script,如果设置了 ignore-scripts,仍会运行其指定的脚本,但它们不会运行任何前置或后置脚本。

🌐 Note that commands explicitly intended to run a particular script, such as npm start, npm stop, npm restart, npm test, and npm run-script will still run their intended script if ignore-scripts is set, but they will not run any pre- or post-scripts.

foreground-scripts

  • 默认:否
  • 类型:布尔

在前台进程中运行已安装包的所有构建脚本(即 preinstallinstallpostinstall 脚本),并与主 npm 进程共享标准输入、输出和错误。

🌐 Run all build scripts (ie, preinstall, install, and postinstall) scripts for installed packages in the foreground process, sharing standard input, output, and error with the main npm process.

请注意,这通常会使安装运行速度变慢,并且噪音更大,但对调试很有用。

🌐 Note that this will generally make installs run slower, and be much noisier, but can be useful for debugging.

script-shell

  • 默认:在 POSIX 系统上为 '/bin/sh',在 Windows 上为 'cmd.exe'
  • 类型:空或字符串

用于通过 npm execnpm runnpm init <package-spec> 命令运行脚本的 shell。

🌐 The shell to use for scripts run with the npm exec, npm run and npm init <package-spec> commands.

也可以看看

🌐 See Also