NPX(1)

πŸ“¦
version 10.9.8 Β· 2 versions
, in fedora 44

πŸ’– NPX(1) πŸ’–

okay bestie, welcome to the NPX(1) page β€” section 1 of the manual, the one and only, and yes we are doing this together. grab a strawberry πŸ“, we're learning.


✨ NAME ✨

npx β€” run a command from a local or remote npm package. that's it, that's the whole vibe. 🫢


🌸 Synopsis 🌸

npx -- <pkg>[@<version>] [args...]
npx --package=<pkg>[@<version>] -- <cmd> [args...]
npx -c '<cmd> [args...]'
npx --package=foo -c '<cmd> [args...]'

πŸ¦‹ Description πŸ¦‹

so basically? this command lets you run an arbitrary command from an npm package β€” could be one that's already installed locally, could be one you're fetching remotely, either way β€” and it runs in a context that's a lot like running it via npm run. same energy, different door. πŸ’…

whatever packages you name with the --package option get dropped into the PATH of the executed command, alongside any locally installed package executables. and the way that --package can be specified multiple times?? iconic. you stack them up and the supplied command runs in an environment where all of those specified packages are available. no cap.

if any of the packages you asked for aren't already sitting in the local project dependencies, then they get installed to a folder in the npm cache, and that folder gets added to the PATH environment variable in the executed process. and before any of that β€” a prompt is printed. you can suppress that prompt by providing either --yes or --no. your call, bestie. πŸ™ˆ

package names given without a specifier get matched against whatever version exists in the local project. package names with a specifier only count as a match if they have the exact same name and version as the local dependency. exact. character for character. we don't do approximations here. ✨

if no -c or --call option is provided, then the positional arguments get used to generate the command string. and if no --package options are provided, npm will attempt to determine the executable name from the package specifier given as the first positional argument β€” using this heuristic, right here:

  • if the package has a single entry in its bin field in package.json, or if all entries are aliases of the same command, then that command will be used.
  • if the package has multiple bin entries, and one of them matches the unscoped portion of the name field, then that command will be used.
  • if this does not result in exactly one option (either because there are no bin entries, or none of them match the name of the package), then npm exec exits with an error. πŸ’€

to run a binary that is not the named binary, specify one or more --package options β€” which stops npm from inferring the package from the first command argument. periodt.


🌈 npx vs npm exec 🌈

when you run it via the npx binary, all flags and options have to be set prior to any positional arguments. that's the rule. when you run it via npm exec, you can use a double-hyphen -- flag to suppress npm's parsing of switches and options that should be sent to the executed command instead.

for example:

$ npx foo@latest bar --package=@npmcli/foo

in this case, npm will resolve the foo package name, and run the following command:

$ foo bar --package=@npmcli/foo

since the --package option comes after the positional arguments, it is treated as an argument to the executed command. sneaky little thing. 🧚

in contrast, because of npm's argument parsing logic, running this command is different:

$ npm exec foo@latest bar --package=@npmcli/foo

in this case, npm parses the --package option first, resolving the @npmcli/foo package. then it executes the following command in that context:

$ foo@latest bar

the double-hyphen character is recommended to explicitly tell npm to stop parsing command line options and switches. so the following command would be equivalent to the npx command above:

$ npm exec -- foo@latest bar --package=@npmcli/foo

see how that works?? lowkey beautiful. 😭


πŸŽ€ Examples πŸŽ€

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

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

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"'

πŸ’œ Compatibility with Older npx Versions πŸ’œ

okay so history lesson, stay with me. the npx binary was rewritten in npm v7.0.0, and the standalone npx package was 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:

  • any npm config value may be provided.
  • to prevent security and user-experience problems from mistyping package names, npx prompts before installing anything. suppress this prompt with the -y or --yes option.
  • the --no-install option is deprecated, and will be converted to --no.
  • shell fallback functionality is removed, as it is not advisable.
  • the -p argument is a shorthand for --parseable in npm, but shorthand for --package in npx. this is maintained, but only for the npx executable. iykyk. πŸ‘‘
  • the --ignore-existing option is removed. locally installed bins are always present in the executed process PATH.
  • the --npm option is removed. npx will always use the npm it ships with.
  • the --node-arg and -n options have been removed. use NODE_OPTIONS (https://nodejs.org/api/cli.html#node_optionsoptions) instead: e.g., NODE_OPTIONS="--trace-warnings --trace-exit" npx foo --random=true
  • the --always-spawn option is redundant, and thus removed.
  • the --shell option is replaced with --script-shell, but maintained in the npx executable for backwards compatibility.

⭐ See Also ⭐

  • npm help run-script
  • npm help scripts
  • npm help test
  • npm help start
  • npm help restart
  • npm help stop
  • npm help config
  • npm help exec

and that's the whole page bestie, you ate that. 🫢✨ this was NPM@10.9.8, from March 2026 β€” go run something cute, mwah πŸ’…πŸŽ€

πŸ“¦ where this page comes from