NPX(1)

πŸ“¦ version 10.8.2, in fedora 43, fedora 44

🌸 NPX 🌸

okay bestie, welcome to the NPX(1) manual page β€” yes that's the header, yes it's giving "I'm a real man page", and yes we're gonna get through the whole thing together πŸ’–

✨ Synopsis ✨

no cap, here's the shape of it:

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

πŸ¦‹ Description πŸ¦‹

so like. this command lets you run an arbitrary command from an npm package β€” could be one that's installed locally, could be one that gets fetched remotely β€” and it runs it in basically the same context as if you'd run it via npm run. main character energy fr fr.

whatever packages you name with the --package option get handed to you in the PATH of the executed command, along with any locally installed package executables. and the --package option is a repeat-queen: you can specify it multiple times, and your command runs in an environment where all of those specified packages are available πŸ‘‘

if any of the packages you asked for aren't in your local project dependencies, they get installed to a folder in the npm cache, and that folder gets added to the PATH environment variable in the executed process. a prompt gets printed for you (which you can suppress by providing either --yes or --no, your call).

package names given without a specifier will be matched with whatever version exists in the local project. package names with a specifier will only count as a match if they have the exact same name and version as the local dependency. periodt.

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 you gave as the first positional argument, using this heuristic:

  • 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.
πŸ’€ heads up: that last one is not a vibe, it's an error. npm exec bails out.

and if you wanna run a binary that is not the named binary, just specify one or more --package options β€” that will prevent npm from inferring the package from the first command argument. slay.


🫢 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. when you run it 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. iykyk.

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's treated as an argument to the executed command. sneaky.

in contrast, due to npm's argument parsing logic, running this command is different:

$ npm exec foo@latest bar --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

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

πŸŽ€ 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 🌈

story time πŸ“ 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.
  • 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.
🫢 rip to the removed options, you will not be missed (except by people with old scripts, love that for them).

πŸ’œ 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, we ate and left no crumbs πŸ’… the version info said NPM@10.8.2 and the date was July 2024 β€” iconic, historic, immaculate. go forth and run your binaries πŸ¦‹βœ¨

πŸ“¦ where this page comes from