.TH "NPX" "1" "November 2022" "9.1.1" .SH "NAME" \fBnpx\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 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 they are installed to a folder in the npm cache, which .br is added to the \fBPATH\fP environment variable in the executed process\. A .br prompt is printed (which can be suppressed by providing either \fB\-\-yes\fP or .br \fB\-\-no\fP)\. .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 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 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 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 exec .RE