11cb0ef41Sopenharmony_ci---
21cb0ef41Sopenharmony_cititle: npx
31cb0ef41Sopenharmony_cisection: 1
41cb0ef41Sopenharmony_cidescription: Run a command from a local or remote npm package
51cb0ef41Sopenharmony_ci---
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci### Synopsis
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci```bash
101cb0ef41Sopenharmony_cinpx -- <pkg>[@<version>] [args...]
111cb0ef41Sopenharmony_cinpx --package=<pkg>[@<version>] -- <cmd> [args...]
121cb0ef41Sopenharmony_cinpx -c '<cmd> [args...]'
131cb0ef41Sopenharmony_cinpx --package=foo -c '<cmd> [args...]'
141cb0ef41Sopenharmony_ci```
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci### Description
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ciThis command allows you to run an arbitrary command from an npm package
191cb0ef41Sopenharmony_ci(either one installed locally, or fetched remotely), in a similar context
201cb0ef41Sopenharmony_cias running it via `npm run`.
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ciWhatever packages are specified by the `--package` option will be
231cb0ef41Sopenharmony_ciprovided in the `PATH` of the executed command, along with any locally
241cb0ef41Sopenharmony_ciinstalled package executables.  The `--package` option may be
251cb0ef41Sopenharmony_cispecified multiple times, to execute the supplied command in an environment
261cb0ef41Sopenharmony_ciwhere all specified packages are available.
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ciIf any requested packages are not present in the local project
291cb0ef41Sopenharmony_cidependencies, then they are installed to a folder in the npm cache, which
301cb0ef41Sopenharmony_ciis added to the `PATH` environment variable in the executed process.  A
311cb0ef41Sopenharmony_ciprompt is printed (which can be suppressed by providing either `--yes` or
321cb0ef41Sopenharmony_ci`--no`).
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ciPackage names provided without a specifier will be matched with whatever
351cb0ef41Sopenharmony_civersion exists in the local project.  Package names with a specifier will
361cb0ef41Sopenharmony_cionly be considered a match if they have the exact same name and version as
371cb0ef41Sopenharmony_cithe local dependency.
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ciIf no `-c` or `--call` option is provided, then the positional arguments
401cb0ef41Sopenharmony_ciare used to generate the command string.  If no `--package` options
411cb0ef41Sopenharmony_ciare provided, then npm will attempt to determine the executable name from
421cb0ef41Sopenharmony_cithe package specifier provided as the first positional argument according
431cb0ef41Sopenharmony_cito the following heuristic:
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci- If the package has a single entry in its `bin` field in `package.json`,
461cb0ef41Sopenharmony_ci  or if all entries are aliases of the same command, then that command
471cb0ef41Sopenharmony_ci  will be used.
481cb0ef41Sopenharmony_ci- If the package has multiple `bin` entries, and one of them matches the
491cb0ef41Sopenharmony_ci  unscoped portion of the `name` field, then that command will be used.
501cb0ef41Sopenharmony_ci- If this does not result in exactly one option (either because there are
511cb0ef41Sopenharmony_ci  no bin entries, or none of them match the `name` of the package), then
521cb0ef41Sopenharmony_ci  `npm exec` exits with an error.
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ciTo run a binary _other than_ the named binary, specify one or more
551cb0ef41Sopenharmony_ci`--package` options, which will prevent npm from inferring the package from
561cb0ef41Sopenharmony_cithe first command argument.
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci### `npx` vs `npm exec`
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ciWhen run via the `npx` binary, all flags and options *must* be set prior to
611cb0ef41Sopenharmony_ciany positional arguments.  When run via `npm exec`, a double-hyphen `--`
621cb0ef41Sopenharmony_ciflag can be used to suppress npm's parsing of switches and options that
631cb0ef41Sopenharmony_cishould be sent to the executed command.
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ciFor example:
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci```
681cb0ef41Sopenharmony_ci$ npx foo@latest bar --package=@npmcli/foo
691cb0ef41Sopenharmony_ci```
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ciIn this case, npm will resolve the `foo` package name, and run the
721cb0ef41Sopenharmony_cifollowing command:
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci```
751cb0ef41Sopenharmony_ci$ foo bar --package=@npmcli/foo
761cb0ef41Sopenharmony_ci```
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ciSince the `--package` option comes _after_ the positional arguments, it is
791cb0ef41Sopenharmony_citreated as an argument to the executed command.
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ciIn contrast, due to npm's argument parsing logic, running this command is
821cb0ef41Sopenharmony_cidifferent:
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ci```
851cb0ef41Sopenharmony_ci$ npm exec foo@latest bar --package=@npmcli/foo
861cb0ef41Sopenharmony_ci```
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ciIn this case, npm will parse the `--package` option first, resolving the
891cb0ef41Sopenharmony_ci`@npmcli/foo` package.  Then, it will execute the following command in that
901cb0ef41Sopenharmony_cicontext:
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci```
931cb0ef41Sopenharmony_ci$ foo@latest bar
941cb0ef41Sopenharmony_ci```
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ciThe double-hyphen character is recommended to explicitly tell npm to stop
971cb0ef41Sopenharmony_ciparsing command line options and switches.  The following command would
981cb0ef41Sopenharmony_cithus be equivalent to the `npx` command above:
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci```
1011cb0ef41Sopenharmony_ci$ npm exec -- foo@latest bar --package=@npmcli/foo
1021cb0ef41Sopenharmony_ci```
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_ci### Examples
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ciRun the version of `tap` in the local dependencies, with the provided
1071cb0ef41Sopenharmony_ciarguments:
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_ci```
1101cb0ef41Sopenharmony_ci$ npm exec -- tap --bail test/foo.js
1111cb0ef41Sopenharmony_ci$ npx tap --bail test/foo.js
1121cb0ef41Sopenharmony_ci```
1131cb0ef41Sopenharmony_ci
1141cb0ef41Sopenharmony_ciRun a command _other than_ the command whose name matches the package name
1151cb0ef41Sopenharmony_ciby specifying a `--package` option:
1161cb0ef41Sopenharmony_ci
1171cb0ef41Sopenharmony_ci```
1181cb0ef41Sopenharmony_ci$ npm exec --package=foo -- bar --bar-argument
1191cb0ef41Sopenharmony_ci# ~ or ~
1201cb0ef41Sopenharmony_ci$ npx --package=foo bar --bar-argument
1211cb0ef41Sopenharmony_ci```
1221cb0ef41Sopenharmony_ci
1231cb0ef41Sopenharmony_ciRun an arbitrary shell script, in the context of the current project:
1241cb0ef41Sopenharmony_ci
1251cb0ef41Sopenharmony_ci```
1261cb0ef41Sopenharmony_ci$ npm x -c 'eslint && say "hooray, lint passed"'
1271cb0ef41Sopenharmony_ci$ npx -c 'eslint && say "hooray, lint passed"'
1281cb0ef41Sopenharmony_ci```
1291cb0ef41Sopenharmony_ci
1301cb0ef41Sopenharmony_ci### Compatibility with Older npx Versions
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ciThe `npx` binary was rewritten in npm v7.0.0, and the standalone `npx`
1331cb0ef41Sopenharmony_cipackage deprecated at that time.  `npx` uses the `npm exec`
1341cb0ef41Sopenharmony_cicommand instead of a separate argument parser and install process, with
1351cb0ef41Sopenharmony_cisome affordances to maintain backwards compatibility with the arguments it
1361cb0ef41Sopenharmony_ciaccepted in previous versions.
1371cb0ef41Sopenharmony_ci
1381cb0ef41Sopenharmony_ciThis resulted in some shifts in its functionality:
1391cb0ef41Sopenharmony_ci
1401cb0ef41Sopenharmony_ci- Any `npm` config value may be provided.
1411cb0ef41Sopenharmony_ci- To prevent security and user-experience problems from mistyping package
1421cb0ef41Sopenharmony_ci  names, `npx` prompts before installing anything.  Suppress this
1431cb0ef41Sopenharmony_ci  prompt with the `-y` or `--yes` option.
1441cb0ef41Sopenharmony_ci- The `--no-install` option is deprecated, and will be converted to `--no`.
1451cb0ef41Sopenharmony_ci- Shell fallback functionality is removed, as it is not advisable.
1461cb0ef41Sopenharmony_ci- The `-p` argument is a shorthand for `--parseable` in npm, but shorthand
1471cb0ef41Sopenharmony_ci  for `--package` in npx.  This is maintained, but only for the `npx`
1481cb0ef41Sopenharmony_ci  executable.
1491cb0ef41Sopenharmony_ci- The `--ignore-existing` option is removed.  Locally installed bins are
1501cb0ef41Sopenharmony_ci  always present in the executed process `PATH`.
1511cb0ef41Sopenharmony_ci- The `--npm` option is removed.  `npx` will always use the `npm` it ships
1521cb0ef41Sopenharmony_ci  with.
1531cb0ef41Sopenharmony_ci- The `--node-arg` and `-n` options have been removed. Use [`NODE_OPTIONS`](https://nodejs.org/api/cli.html#node_optionsoptions) instead: e.g.,
1541cb0ef41Sopenharmony_ci `NODE_OPTIONS="--trace-warnings --trace-exit" npx foo --random=true`
1551cb0ef41Sopenharmony_ci- The `--always-spawn` option is redundant, and thus removed.
1561cb0ef41Sopenharmony_ci- The `--shell` option is replaced with `--script-shell`, but maintained
1571cb0ef41Sopenharmony_ci  in the `npx` executable for backwards compatibility.
1581cb0ef41Sopenharmony_ci
1591cb0ef41Sopenharmony_ci### See Also
1601cb0ef41Sopenharmony_ci
1611cb0ef41Sopenharmony_ci* [npm run-script](/commands/npm-run-script)
1621cb0ef41Sopenharmony_ci* [npm scripts](/using-npm/scripts)
1631cb0ef41Sopenharmony_ci* [npm test](/commands/npm-test)
1641cb0ef41Sopenharmony_ci* [npm start](/commands/npm-start)
1651cb0ef41Sopenharmony_ci* [npm restart](/commands/npm-restart)
1661cb0ef41Sopenharmony_ci* [npm stop](/commands/npm-stop)
1671cb0ef41Sopenharmony_ci* [npm config](/commands/npm-config)
1681cb0ef41Sopenharmony_ci* [npm exec](/commands/npm-exec)
169