deprecate-totp-2fa
@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/v7 |
| 7 | github_path: docs/content/commands/npx.md |
| 8 | redirect_from: |
| 9 | - /cli-documentation/v7/cli-commands/npx |
| 10 | - /cli-documentation/v7/commands/npx |
| 11 | - /cli-documentation/v7/npx |
| 12 | - /cli/v7/cli-commands/npx |
| 13 | - /cli/v7/npx |
| 14 | --- |
| 15 | |
| 16 | ### Synopsis |
| 17 | |
| 18 | ```bash |
| 19 | npm exec -- <pkg>[@<version>] [args...] |
| 20 | npm exec --package=<pkg>[@<version>] -- <cmd> [args...] |
| 21 | npm exec -c '<cmd> [args...]' |
| 22 | npm exec --package=foo -c '<cmd> [args...]' |
| 23 | |
| 24 | npx <pkg>[@<specifier>] [args...] |
| 25 | npx -p <pkg>[@<specifier>] <cmd> [args...] |
| 26 | npx -c '<cmd> [args...]' |
| 27 | npx -p <pkg>[@<specifier>] -c '<cmd> [args...]' |
| 28 | |
| 29 | alias: npm x, npx |
| 30 | |
| 31 | --package=<pkg> (may be specified multiple times) |
| 32 | -p is a shorthand for --package only when using npx executable |
| 33 | -c <cmd> --call=<cmd> (may not be mixed with positional arguments) |
| 34 | ``` |
| 35 | |
| 36 | ### Description |
| 37 | |
| 38 | 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`. |
| 39 | |
| 40 | 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. |
| 41 | |
| 42 | 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`). |
| 43 | |
| 44 | 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. |
| 45 | |
| 46 | 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: |
| 47 | |
| 48 | - 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. |
| 49 | - 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. |
| 50 | - 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. |
| 51 | |
| 52 | 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. |
| 53 | |
| 54 | ### `npx` vs `npm exec` |
| 55 | |
| 56 | 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. |
| 57 | |
| 58 | For example: |
| 59 | |
| 60 | ``` |
| 61 | $ npx foo@latest bar --package=@npmcli/foo |
| 62 | ``` |
| 63 | |
| 64 | In this case, npm will resolve the `foo` package name, and run the following command: |
| 65 | |
| 66 | ``` |
| 67 | $ foo bar --package=@npmcli/foo |
| 68 | ``` |
| 69 | |
| 70 | Since the `--package` option comes _after_ the positional arguments, it is treated as an argument to the executed command. |
| 71 | |
| 72 | In contrast, due to npm's argument parsing logic, running this command is different: |
| 73 | |
| 74 | ``` |
| 75 | $ npm exec foo@latest bar --package=@npmcli/foo |
| 76 | ``` |
| 77 | |
| 78 | 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: |
| 79 | |
| 80 | ``` |
| 81 | $ foo@latest bar |
| 82 | ``` |
| 83 | |
| 84 | 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: |
| 85 | |
| 86 | ``` |
| 87 | $ npm exec -- foo@latest bar --package=@npmcli/foo |
| 88 | ``` |
| 89 | |
| 90 | ### Examples |
| 91 | |
| 92 | Run the version of `tap` in the local dependencies, with the provided arguments: |
| 93 | |
| 94 | ``` |
| 95 | $ npm exec -- tap --bail test/foo.js |
| 96 | $ npx tap --bail test/foo.js |
| 97 | ``` |
| 98 | |
| 99 | Run a command _other than_ the command whose name matches the package name by specifying a `--package` option: |
| 100 | |
| 101 | ``` |
| 102 | $ npm exec --package=foo -- bar --bar-argument |
| 103 | # ~ or ~ |
| 104 | $ npx --package=foo bar --bar-argument |
| 105 | ``` |
| 106 | |
| 107 | Run an arbitrary shell script, in the context of the current project: |
| 108 | |
| 109 | ``` |
| 110 | $ npm x -c 'eslint && say "hooray, lint passed"' |
| 111 | $ npx -c 'eslint && say "hooray, lint passed"' |
| 112 | ``` |
| 113 | |
| 114 | ### Compatibility with Older npx Versions |
| 115 | |
| 116 | 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. |
| 117 | |
| 118 | This resulted in some shifts in its functionality: |
| 119 | |
| 120 | - Any `npm` config value may be provided. |
| 121 | - 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. |
| 122 | - The `--no-install` option is deprecated, and will be converted to `--no`. |
| 123 | - Shell fallback functionality is removed, as it is not advisable. |
| 124 | - 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. |
| 125 | - The `--ignore-existing` option is removed. Locally installed bins are always present in the executed process `PATH`. |
| 126 | - The `--npm` option is removed. `npx` will always use the `npm` it ships with. |
| 127 | - The `--node-arg` and `-n` options are removed. |
| 128 | - The `--always-spawn` option is redundant, and thus removed. |
| 129 | - The `--shell` option is replaced with `--script-shell`, but maintained in the `npx` executable for backwards compatibility. |
| 130 | |
| 131 | ### See Also |
| 132 | |
| 133 | - [npm run-script](/cli/v7/commands/npm-run-script) |
| 134 | - [npm scripts](/cli/v7/using-npm/scripts) |
| 135 | - [npm test](/cli/v7/commands/npm-test) |
| 136 | - [npm start](/cli/v7/commands/npm-start) |
| 137 | - [npm restart](/cli/v7/commands/npm-restart) |
| 138 | - [npm stop](/cli/v7/commands/npm-stop) |
| 139 | - [npm config](/cli/v7/commands/npm-config) |