.TH "NPM\-EXEC" "1" "December 2022" "9.2.0" .SH "NAME" \fBnpm-exec\fR .SH Synopsis .SH Description .P This command allows you to run an arbitrary command from an npm package .br (either one installed locally, or fetched remotely), in a similar context .br as running it via \fBnpm run\fP\|\. .P Run without positional arguments or \fB\-\-call\fP, this allows you to .br interactively run commands in the same sort of shell environment that .br \fBpackage\.json\fP scripts are run\. Interactive mode is not supported in CI .br environments when standard input is a TTY, to prevent hangs\. .P Whatever packages are specified by the \fB\-\-package\fP option will be .br provided in the \fBPATH\fP of the executed command, along with any locally .br installed package executables\. The \fB\-\-package\fP option may be .br specified multiple times, to execute the supplied command in an environment .br where all specified packages are available\. .P If any requested packages are not present in the local project .br dependencies, then a prompt is printed, which can be suppressed by .br providing either \fB\-\-yes\fP or \fB\-\-no\fP\|\. When standard input is not a TTY or a .br CI environment is detected, \fB\-\-yes\fP is assumed\. The requested packages are .br installed to a folder in the npm cache, which is added to the \fBPATH\fP .br environment variable in the executed process\. .P Package names provided without a specifier will be matched with whatever .br version exists in the local project\. Package names with a specifier will .br only be considered a match if they have the exact same name and version as .br the local dependency\. .P If no \fB\-c\fP or \fB\-\-call\fP option is provided, then the positional arguments .br are used to generate the command string\. If no \fB\-\-package\fP options .br are provided, then npm will attempt to determine the executable name from .br the package specifier provided as the first positional argument according .br to the following heuristic: .RS 1 .IP \(bu 2 If the package has a single entry in its \fBbin\fP field in \fBpackage\.json\fP, .br or if all entries are aliases of the same command, then that command .br will be used\. .IP \(bu 2 If the package has multiple \fBbin\fP entries, and one of them matches the .br unscoped portion of the \fBname\fP field, then that command will be used\. .IP \(bu 2 If this does not result in exactly one option (either because there are .br no bin entries, or none of them match the \fBname\fP of the package), then .br \fBnpm exec\fP exits with an error\. .RE .P To run a binary \fIother than\fR the named binary, specify one or more .br \fB\-\-package\fP options, which will prevent npm from inferring the package from .br the first command argument\. .SH \fBnpx\fP vs \fBnpm exec\fP .P When run via the \fBnpx\fP binary, all flags and options \fImust\fR be set prior to .br any positional arguments\. When run via \fBnpm exec\fP, a double\-hyphen \fB\-\-\fP .br flag can be used to suppress npm's parsing of switches and options that .br should be sent to the executed command\. .P For example: .RS 2 .nf $ npx foo@latest bar \-\-package=@npmcli/foo .fi .RE .P In this case, npm will resolve the \fBfoo\fP package name, and run the .br following command: .RS 2 .nf $ foo bar \-\-package=@npmcli/foo .fi .RE .P Since the \fB\-\-package\fP option comes \fIafter\fR the positional arguments, it is .br treated as an argument to the executed command\. .P In contrast, due to npm's argument parsing logic, running this command is .br different: .RS 2 .nf $ npm exec foo@latest bar \-\-package=@npmcli/foo .fi .RE .P In this case, npm will parse the \fB\-\-package\fP option first, resolving the .br \fB@npmcli/foo\fP package\. Then, it will execute the following command in that .br context: .RS 2 .nf $ foo@latest bar .fi .RE .P The double\-hyphen character is recommended to explicitly tell npm to stop .br parsing command line options and switches\. The following command would .br thus be equivalent to the \fBnpx\fP command above: .RS 2 .nf $ npm exec \-\- foo@latest bar \-\-package=@npmcli/foo .fi .RE .SH Configuration .SH Examples .P Run the version of \fBtap\fP in the local dependencies, with the provided .br arguments: .RS 2 .nf $ npm exec \-\- tap \-\-bail test/foo\.js $ npx tap \-\-bail test/foo\.js .fi .RE .P Run a command \fIother than\fR the command whose name matches the package name .br by specifying a \fB\-\-package\fP option: .RS 2 .nf $ npm exec \-\-package=foo \-\- bar \-\-bar\-argument # ~ or ~ $ npx \-\-package=foo bar \-\-bar\-argument .fi .RE .P Run an arbitrary shell script, in the context of the current project: .RS 2 .nf $ npm x \-c 'eslint && say "hooray, lint passed"' $ npx \-c 'eslint && say "hooray, lint passed"' .fi .RE .SH Workspaces support .P You may use the \fBworkspace\fP or .br \fBworkspaces\fP configs in order to run an .br arbitrary command from an npm package (either one installed locally, or fetched .br remotely) in the context of the specified workspaces\. .br If no positional argument or \fB\-\-call\fP option is provided, it will open an .br interactive subshell in the context of each of these configured workspaces one .br at a time\. .P Given a project with configured workspaces, e\.g: .RS 2 .nf \|\. +\-\- package\.json `\-\- packages +\-\- a | `\-\- package\.json +\-\- b | `\-\- package\.json `\-\- c `\-\- package\.json .fi .RE .P Assuming the workspace configuration is properly set up at the root level .br \fBpackage\.json\fP file\. e\.g: .RS 2 .nf { "workspaces": [ "\./packages/*" ] } .fi .RE .P You can execute an arbitrary command from a package in the context of each of .br the configured workspaces when using the .br \fBworkspaces\fP config options, in this example .br we're using \fBeslint\fR to lint any js file found within each workspace folder: .RS 2 .nf npm exec \-\-ws \-\- eslint \./*\.js .fi .RE .SS Filtering workspaces .P It's also possible to execute a command in a single workspace using the .br \fBworkspace\fP config along with a name or directory path: .RS 2 .nf npm exec \-\-workspace=a \-\- eslint \./*\.js .fi .RE .P The \fBworkspace\fP config can also be specified multiple times in order to run a .br specific script in the context of multiple workspaces\. When defining values for .br the \fBworkspace\fP config in the command line, it also possible to use \fB\-w\fP as a .br shorthand, e\.g: .RS 2 .nf npm exec \-w a \-w b \-\- eslint \./*\.js .fi .RE .P This last command will run the \fBeslint\fP command in both \fB\|\./packages/a\fP and .br \fB\|\./packages/b\fP folders\. .SH Compatibility with Older npx Versions .P The \fBnpx\fP binary was rewritten in npm v7\.0\.0, and the standalone \fBnpx\fP .br package deprecated at that time\. \fBnpx\fP uses the \fBnpm exec\fP .br command instead of a separate argument parser and install process, with .br some affordances to maintain backwards compatibility with the arguments it .br accepted in previous versions\. .P This resulted in some shifts in its functionality: .RS 1 .IP \(bu 2 Any \fBnpm\fP config value may be provided\. .IP \(bu 2 To prevent security and user\-experience problems from mistyping package .br names, \fBnpx\fP prompts before installing anything\. Suppress this .br prompt with the \fB\-y\fP or \fB\-\-yes\fP option\. .IP \(bu 2 The \fB\-\-no\-install\fP option is deprecated, and will be converted to \fB\-\-no\fP\|\. .IP \(bu 2 Shell fallback functionality is removed, as it is not advisable\. .IP \(bu 2 The \fB\-p\fP argument is a shorthand for \fB\-\-parseable\fP in npm, but shorthand .br for \fB\-\-package\fP in npx\. This is maintained, but only for the \fBnpx\fP .br executable\. .IP \(bu 2 The \fB\-\-ignore\-existing\fP option is removed\. Locally installed bins are .br always present in the executed process \fBPATH\fP\|\. .IP \(bu 2 The \fB\-\-npm\fP option is removed\. \fBnpx\fP will always use the \fBnpm\fP it ships .br with\. .IP \(bu 2 The \fB\-\-node\-arg\fP and \fB\-n\fP options are removed\. .IP \(bu 2 The \fB\-\-always\-spawn\fP option is redundant, and thus removed\. .IP \(bu 2 The \fB\-\-shell\fP option is replaced with \fB\-\-script\-shell\fP, but maintained .br in the \fBnpx\fP executable for backwards compatibility\. .RE .SH A note on caching .P The npm cli utilizes its internal package cache when using the package .br name specified\. You can use the following to change how and when the .br cli uses this cache\. See \fBnpm cache\fP for more on .br how the cache works\. .SS prefer\-online .P Forces staleness checks for packages, making the cli look for updates .br immediately even if the package is already in the cache\. .SS prefer\-offline .P Bypasses staleness checks for packages\. Missing data will still be .br requested from the server\. To force full offline mode, use \fBoffline\fP\|\. .SS offline .P Forces full offline mode\. Any packages not locally cached will result in .br an error\. .SS workspace .RS 1 .IP \(bu 2 Default: .IP \(bu 2 Type: String (can be set multiple times) .RE .P Enable running a command in the context of the configured workspaces of the .br current project while filtering by running only the workspaces defined by .br this configuration option\. .P Valid values for the \fBworkspace\fP config are either: .RS 1 .IP \(bu 2 Workspace names .IP \(bu 2 Path to a workspace directory .IP \(bu 2 Path to a parent workspace directory (will result to selecting all of the .br nested workspaces) .RE .P This value is not exported to the environment for child processes\. .SS workspaces .RS 1 .IP \(bu 2 Alias: \fB\-\-ws\fP .IP \(bu 2 Type: Boolean .IP \(bu 2 Default: \fBfalse\fP .RE .P Run scripts in the context of all configured workspaces for the current .br project\. .SH See Also .RS 1 .IP \(bu 2 npm run\-script .IP \(bu 2 npm scripts .IP \(bu 2 npm test .IP \(bu 2 npm start .IP \(bu 2 npm restart .IP \(bu 2 npm stop .IP \(bu 2 npm config .IP \(bu 2 npm workspaces .IP \(bu 2 npx .RE