jpg619/fix-accessibility-content-flow
@reggi/path-to-regexp
dependabot/npm_and_yarn/main/copy-to-clipboard-4.0.2
dependabot/npm_and_yarn/main/eslint-10.4.0
dependabot/npm_and_yarn/main/npmcli/eslint-config-7.0.0
dependabot/npm_and_yarn/main/proc-log-7.0.0
dependabot/npm_and_yarn/npm_and_yarn-826852524d
dependabot/npm_and_yarn/npm_and_yarn-ab9a7f4bc2
deprecate-totp-2fa
dhei/classic-tokens
gat-bypass-2fa-docs
jpg619/fix-accessibility-content-flow
jpg619/version-bump-tar-2
kartykp/gat-bypass-2fa-docs
kartykp/upgrade-path-to-regex
main
maitxn/version-bump-tar
patch-1
reggi/cache-based-on-version
reggi/dev-engines
reggi/fix-transform-prettier
reggi/overrides
update-search-sensitivity
| 1 | --- |
| 2 | title: npx |
| 3 | section: 1 |
| 4 | description: Run a command from a local or remote npm package |
| 5 | github_repo: npm/cli |
| 6 | github_branch: release/v10 |
| 7 | github_path: docs/lib/content/commands/npx.md |
| 8 | redirect_from: |
| 9 | - /cli-documentation/v10/cli-commands/npx |
| 10 | - /cli-documentation/v10/commands/npx |
| 11 | - /cli-documentation/v10/npx |
| 12 | - /cli/v10/cli-commands/npx |
| 13 | - /cli/v10/npx |
| 14 | --- |
| 15 | |
| 16 | ### Synopsis |
| 17 | |
| 18 | ```bash |
| 19 | npx -- <pkg>[@<version>] [args...] |
| 20 | npx --package=<pkg>[@<version>] -- <cmd> [args...] |
| 21 | npx -c '<cmd> [args...]' |
| 22 | npx --package=foo -c '<cmd> [args...]' |
| 23 | ``` |
| 24 | |
| 25 | ### Description |
| 26 | |
| 27 | This command allows you to run an arbitrary command from an npm package (either one installed locally, or fetched remotely), in a similar context as running it via `npm run`. |
| 28 | |
| 29 | Whatever packages are specified by the `--package` option will be provided in the `PATH` of the executed command, along with any locally installed package executables. The `--package` option may be specified multiple times, to execute the supplied command in an environment where all specified packages are available. |
| 30 | |
| 31 | If any requested packages are not present in the local project dependencies, then they are installed to a folder in the npm cache, which is added to the `PATH` environment variable in the executed process. A prompt is printed (which can be suppressed by providing either `--yes` or `--no`). |
| 32 | |
| 33 | Package names provided without a specifier will be matched with whatever version exists in the local project. Package names with a specifier will only be considered a match if they have the exact same name and version as the local dependency. |
| 34 | |
| 35 | If no `-c` or `--call` option is provided, then the positional arguments are used to generate the command string. If no `--package` options are provided, then npm will attempt to determine the executable name from the package specifier provided as the first positional argument according to the following heuristic: |
| 36 | |
| 37 | - 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. |
| 38 | - 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. |
| 39 | - 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. |
| 40 | |
| 41 | To run a binary _other than_ the named binary, specify one or more `--package` options, which will prevent npm from inferring the package from the first command argument. |
| 42 | |
| 43 | ### `npx` vs `npm exec` |
| 44 | |
| 45 | When run via the `npx` binary, all flags and options _must_ be set prior to any positional arguments. When run 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. |
| 46 | |
| 47 | For example: |
| 48 | |
| 49 | ``` |
| 50 | $ npx foo@latest bar --package=@npmcli/foo |
| 51 | ``` |
| 52 | |
| 53 | In this case, npm will resolve the `foo` package name, and run the following command: |
| 54 | |
| 55 | ``` |
| 56 | $ foo bar --package=@npmcli/foo |
| 57 | ``` |
| 58 | |
| 59 | Since the `--package` option comes _after_ the positional arguments, it is treated as an argument to the executed command. |
| 60 | |
| 61 | In contrast, due to npm's argument parsing logic, running this command is different: |
| 62 | |
| 63 | ``` |
| 64 | $ npm exec foo@latest bar --package=@npmcli/foo |
| 65 | ``` |
| 66 | |
| 67 | 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: |
| 68 | |
| 69 | ``` |
| 70 | $ foo@latest bar |
| 71 | ``` |
| 72 | |
| 73 | 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: |
| 74 | |
| 75 | ``` |
| 76 | $ npm exec -- foo@latest bar --package=@npmcli/foo |
| 77 | ``` |
| 78 | |
| 79 | ### Examples |
| 80 | |
| 81 | Run the version of `tap` in the local dependencies, with the provided arguments: |
| 82 | |
| 83 | ``` |
| 84 | $ npm exec -- tap --bail test/foo.js |
| 85 | $ npx tap --bail test/foo.js |
| 86 | ``` |
| 87 | |
| 88 | Run a command _other than_ the command whose name matches the package name by specifying a `--package` option: |
| 89 | |
| 90 | ``` |
| 91 | $ npm exec --package=foo -- bar --bar-argument |
| 92 | # ~ or ~ |
| 93 | $ npx --package=foo bar --bar-argument |
| 94 | ``` |
| 95 | |
| 96 | Run an arbitrary shell script, in the context of the current project: |
| 97 | |
| 98 | ``` |
| 99 | $ npm x -c 'eslint && say "hooray, lint passed"' |
| 100 | $ npx -c 'eslint && say "hooray, lint passed"' |
| 101 | ``` |
| 102 | |
| 103 | ### Compatibility with Older npx Versions |
| 104 | |
| 105 | The `npx` binary was rewritten in npm v7.0.0, and the standalone `npx` package 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. |
| 106 | |
| 107 | This resulted in some shifts in its functionality: |
| 108 | |
| 109 | - Any `npm` config value may be provided. |
| 110 | - 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. |
| 111 | - The `--no-install` option is deprecated, and will be converted to `--no`. |
| 112 | - Shell fallback functionality is removed, as it is not advisable. |
| 113 | - 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. |
| 114 | - The `--ignore-existing` option is removed. Locally installed bins are always present in the executed process `PATH`. |
| 115 | - The `--npm` option is removed. `npx` will always use the `npm` it ships with. |
| 116 | - 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` |
| 117 | - The `--always-spawn` option is redundant, and thus removed. |
| 118 | - The `--shell` option is replaced with `--script-shell`, but maintained in the `npx` executable for backwards compatibility. |
| 119 | |
| 120 | ### See Also |
| 121 | |
| 122 | - [npm run-script](/cli/v10/commands/npm-run-script) |
| 123 | - [npm scripts](/cli/v10/using-npm/scripts) |
| 124 | - [npm test](/cli/v10/commands/npm-test) |
| 125 | - [npm start](/cli/v10/commands/npm-start) |
| 126 | - [npm restart](/cli/v10/commands/npm-restart) |
| 127 | - [npm stop](/cli/v10/commands/npm-stop) |
| 128 | - [npm config](/cli/v10/commands/npm-config) |
| 129 | - [npm exec](/cli/v10/commands/npm-exec) |