选择命令行版本:
🌐 Synopsis
npm exec -- <pkg>[@<version>] [args...]npm exec --package=<pkg>[@<version>] -- <cmd> [args...]npm exec -c '<cmd> [args...]'npm exec --package=foo -c '<cmd> [args...]'alias: x
🌐 Description
此命令允许你从 npm 包运行任意命令(无论是本地安装的还是远程获取的),其运行环境与通过 npm run 运行时类似。
🌐 This command allows you to run an arbitrary command from an npm package (either one installed locally, or fetched remotely), in a similar context as running it via npm run.
在不使用位置参数或 --call 的情况下运行,这允许你在与 package.json 脚本运行时相同的 shell 环境中交互式地运行命令。当标准输入是 TTY 时,交互模式在 CI 环境中不受支持,以防止挂起。
🌐 Run without positional arguments or --call, this allows you to interactively run commands in the same sort of shell environment that package.json scripts are run. Interactive mode is not supported in CI environments when standard input is a TTY, to prevent hangs.
通过 --package 选项指定的任何软件包都将与任何本地安装的软件包可执行文件一起,在执行的命令的 PATH 中提供。可以多次指定 --package 选项,以在提供的命令所需的环境中使用所有指定的软件包。
🌐 Whatever packages are specified by the --package option will be provided in the PATH of the executed command, along with any locally installed package executables. The --package option may be specified multiple times, to execute the supplied command in an environment where all specified packages are available.
如果本地项目依赖中不存在任何请求的软件包,则会打印提示,可以通过提供 --yes 或 --no 来抑制该提示。当标准输入不是 TTY 或检测到 CI 环境时,将假定为 --yes。请求的软件包将安装到 npm 缓存中的一个文件夹中,该文件夹会被添加到执行进程的 PATH 环境变量中。
🌐 If any requested packages are not present in the local project dependencies, then a prompt is printed, which can be suppressed by providing either --yes or --no. When standard input is not a TTY or a CI environment is detected, --yes is assumed. The requested packages are installed to a folder in the npm cache, which is added to the PATH environment variable in the executed process.
未提供规范的包名将与本地项目中存在的任何版本匹配。带有规范的包名只有在与本地依赖的名称和版本完全相同时,才会被视为匹配。
🌐 Package names provided without a specifier will be matched with whatever version exists in the local project. Package names with a specifier will only be considered a match if they have the exact same name and version as the local dependency.
如果未提供 -c 或 --call 选项,则使用位置参数来生成命令字符串。如果未提供 --package 选项,则 npm 将根据以下启发式规则尝试从作为第一个位置参数提供的软件包说明符中确定可执行文件的名称:
🌐 If no -c or --call option is provided, then the positional arguments are used to generate the command string. If no --package options are provided, then npm will attempt to determine the executable name from the package specifier provided as the first positional argument according to the following heuristic:
package.json 的 bin 字段中只有一个条目,或者所有条目都是同一命令的别名,那么将使用该命令。bin 条目,并且其中一个与 name 字段的无作用域部分匹配,那么将使用该命令。name),那么 npm exec 将以错误退出。要运行除指定二进制文件之外的二进制文件,请指定一个或多个 --package 选项,这将阻止 npm 从第一个命令参数推断包。
🌐 To run a binary other than the named binary, specify one or more --package options, which will prevent npm from inferring the package from the first command argument.
npx 对比 npm exec🌐 npx vs npm exec
通过 npx 可执行文件运行时,所有标志和选项必须在任何位置参数之前设置。通过 npm exec 运行时,可以使用双连字符 -- 标志来阻止 npm 解析应发送到执行命令的开关和选项。
🌐 When run via the npx binary, all flags and options must be set prior to any positional arguments. When run via npm exec, a double-hyphen -- flag can be used to suppress npm's parsing of switches and options that should be sent to the executed command.
例如:
🌐 For example:
$ npx foo@latest bar --package=@npmcli/foo
在这种情况下,npm 将解析 foo 包名称,并运行以下命令:
🌐 In this case, npm will resolve the foo package name, and run the following command:
$ foo bar --package=@npmcli/foo
由于 --package 选项位于位置参数之后,它被视为传递给已执行命令的参数。
🌐 Since the --package option comes after the positional arguments, it is treated as an argument to the executed command.
相比之下,由于 npm 的参数解析逻辑,运行这个命令是不同的:
🌐 In contrast, due to npm's argument parsing logic, running this command is different:
$ npm exec foo@latest bar --package=@npmcli/foo
在这种情况下,npm 会先解析 --package 选项,解析 @npmcli/foo 包。然后,它将在该上下文中执行以下命令:
🌐 In this case, npm will parse the --package option first, resolving the @npmcli/foo package. Then, it will execute the following command in that context:
$ foo@latest bar
建议使用双连字符来明确告诉 npm 停止解析命令行选项和开关。因此,以下命令将等同于上面的 npx 命令:
🌐 The double-hyphen character is recommended to explicitly tell npm to stop parsing command line options and switches. The following command would thus be equivalent to the npx command above:
$ npm exec -- foo@latest bar --package=@npmcli/foo
🌐 Configuration
package要为 npm exec 安装的软件包
🌐 The package or packages to install for npm exec
callnpm exec、npx 的可选伴随选项,允许指定一个自定义命令与已安装的软件包一起运行。
🌐 Optional companion option for npm exec, npx that allows for specifying a custom command to be run along with the installed packages.
npm exec --package yo --package generator-node --call "yo node"
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.
🌐 Examples
使用本地依赖中的 tap 版本,并提供以下参数运行:
🌐 Run the version of tap in the local dependencies, with the provided arguments:
$ npm exec -- tap --bail test/foo.js$ npx tap --bail test/foo.js
通过指定 --package 选项来运行与包名匹配的命令以外的其他命令:
🌐 Run a command other than the command whose name matches the package name by specifying a --package option:
$ npm exec --package=foo -- bar --bar-argument# ~ or ~$ npx --package=foo bar --bar-argument
在当前项目的上下文中运行任意 shell 脚本:
🌐 Run an arbitrary shell script, in the context of the current project:
$ npm x -c 'eslint && say "hooray, lint passed"'$ npx -c 'eslint && say "hooray, lint passed"'
🌐 Workspaces support
你可以使用 workspace 或 workspaces 配置来在指定的工作区上下文中运行来自 npm 包的任意命令(无论是本地安装的还是远程获取的)。如果未提供位置参数或 --call 选项,它将依次在每个配置的工作区上下文中打开一个交互式子 shell。
🌐 You may use the workspace or workspaces configs in order to run an arbitrary command from an npm package (either one installed locally, or fetched remotely) in the context of the specified workspaces. If no positional argument or --call option is provided, it will open an interactive subshell in the context of each of these configured workspaces one at a time.
给定一个具有已配置工作区的项目,例如:
🌐 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/*" ]}
当使用 workspaces 配置选项 时,你可以在每个配置的工作区上下文中执行包中的任意命令,在这个示例中,我们使用 eslint 对每个工作区文件夹中找到的任何 js 文件进行 lint:
🌐 You can execute an arbitrary command from a package in the context of each of the configured workspaces when using the workspaces config options, in this example we're using eslint to lint any js file found within each workspace folder:
npm exec --ws -- eslint ./*.js
🌐 Filtering workspaces
也可以使用 workspace 配置以及名称或目录路径在单个工作区中执行命令:
🌐 It's also possible to execute a command in a single workspace using the workspace config along with a name or directory path:
npm exec --workspace=a -- eslint ./*.js
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 exec -w a -w b -- eslint ./*.js
最后这个命令将在 ./packages/a 和 ./packages/b 文件夹中运行 eslint 命令。
🌐 This last command will run the eslint command in both ./packages/a and ./packages/b folders.
🌐 Compatibility with Older npx Versions
npx 二进制文件在 npm v7.0.0 中被重写,同时独立的 npx 包在那时被弃用。npx 使用 npm exec 命令,而不是单独的参数解析器和安装流程,并提供了一些机制以保持与之前版本接受的参数的向后兼容性。
🌐 The npx binary was rewritten in npm v7.0.0, and the standalone npx package deprecated at that time. npx uses the npm exec command instead of a separate argument parser and install process, with some affordances to maintain backwards compatibility with the arguments it accepted in previous versions.
这导致其功能发生了一些变化:
🌐 This resulted in some shifts in its functionality:
npm 配置值。npx 会在安装任何东西之前提示确认。可以使用 -y 或 --yes 选项来取消此提示。--no-install 选项已被弃用,将转换为 --no。-p 参数在 npm 中是 --parseable 的简写,但在 npx 中是 --package 的简写。这是被保留的,但仅适用于 npx 可执行文件。--ignore-existing 选项已被移除。本地安装的二进制文件始终存在于执行的进程 PATH 中。--npm 选项已被移除。npx 将始终使用其自带的 npm。--node-arg 和 -n 选项已被移除。--always-spawn 选项是多余的,因此已被移除。--shell 选项被 --script-shell 替代,但为了向后兼容,仍保留在 npx 可执行文件中。🌐 A note on caching
当使用指定的软件包名称时,npm CLI 会利用其内部的软件包缓存。你可以使用以下方法来更改 CLI 使用此缓存的方式和时间。有关缓存工作原理的更多信息,请参见 npm cache。
🌐 The npm cli utilizes its internal package cache when using the package name specified. You can use the following to change how and when the cli uses this cache. See npm cache for more on how the cache works.
强制检查包的时效性,使 cli 立即查找更新,即使包已经在缓存中。
🌐 Forces staleness checks for packages, making the cli look for updates immediately even if the package is already in the cache.
绕过软件包的陈旧性检查。缺失的数据仍会从服务器请求。如需强制完全离线模式,请使用 offline。
🌐 Bypasses staleness checks for packages. Missing data will still be requested from the server. To force full offline mode, use offline.
强制完全离线模式。任何未在本地缓存的包都会导致错误。
🌐 Forces full offline mode. Any packages not locally cached will result in an error.
启用在当前项目的已配置工作区的上下文中运行命令,同时通过仅运行此配置选项定义的工作区进行过滤。
🌐 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:
此值不会导出到子进程的环境中。
🌐 This value is not exported to the environment for child processes.
--wsfalse在当前项目的所有已配置工作区的上下文中运行脚本。
🌐 Run scripts in the context of all configured workspaces for the current project.
🌐 See Also