NPX(1)
πΈ 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
binfield inpackage.json, or if all entries are aliases of the same command, then that command will be used. - If the package has multiple
binentries, and one of them matches the unscoped portion of thenamefield, then that command will be used. - If this does not result in exactly one option (either because there are no
binentries, or none of them match the name of the package), thennpm execexits 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,
npxprompts before installing anything. Suppress this prompt with the-yor--yesoption. - The
--no-installoption is deprecated, and will be converted to--no. - Shell fallback functionality is removed, as it is not advisable.
- The
-pargument is a shorthand for--parseablein npm, but shorthand for--packageinnpx. This is maintained, but only for thenpxexecutable. - The
--ignore-existingoption is removed. Locally installed bins are always present in the executed processPATH. - The
--npmoption is removed.npxwill always use the npm it ships with. - The
--node-argand-noptions have been removed. UseNODE_OPTIONShttps://nodejs.org/api/cli.html#node_optionsoptionsinstead: e.g.,NODE_OPTIONS="--trace-warnings --trace-exit" npx foo --random=true - The
--always-spawnoption is redundant, and thus removed. - The
--shelloption is replaced with--script-shell, but maintained in thenpxexecutable 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-scriptnpm help scriptsnpm help testnpm help startnpm help restartnpm help stopnpm help confignpm 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
- fedora 43: nodejs20-npm 1:10.8.2-1.20.20.2.3.fc43 Β· Apache-2.0 AND Artistic-2.0 AND BSD-2-Clause AND BSD-3-Clause AND BlueOak-1.0.0 AND CC-BY-3.0 AND CC0-1.0 AND ISC AND MIT
- fedora 44: nodejs20-npm 1:10.8.2-1.20.20.0.5.fc44 Β· Apache-2.0 AND Artistic-2.0 AND BSD-2-Clause AND BSD-3-Clause AND BlueOak-1.0.0 AND CC-BY-3.0 AND CC0-1.0 AND ISC AND MIT
- fedora 44: nodejs20-npm 1:10.8.2-1.20.20.2.3.fc44 Β· Apache-2.0 AND Artistic-2.0 AND BSD-2-Clause AND BSD-3-Clause AND BlueOak-1.0.0 AND CC-BY-3.0 AND CC0-1.0 AND ISC AND MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms