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