1 ---
2 title: npm-exec
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/npm-exec.md
8 redirect_from:
9 - /cli-documentation/v7/cli-commands/exec
10 - /cli-documentation/v7/cli-commands/npm-exec
11 - /cli-documentation/v7/commands/exec
12 - /cli-documentation/v7/commands/npm-exec
13 - /cli-documentation/v7/exec
14 - /cli-documentation/v7/npm-exec
15 - /cli/v7/cli-commands/exec
16 - /cli/v7/cli-commands/npm-exec
17 - /cli/v7/commands/exec
18 - /cli/v7/exec
19 - /cli/v7/npm-exec
20 ---
21
22 ### Synopsis
23
24 ```bash
25 npm exec -- <pkg>[@<version>] [args...]
26 npm exec --package=<pkg>[@<version>] -- <cmd> [args...]
27 npm exec -c '<cmd> [args...]'
28 npm exec --package=foo -c '<cmd> [args...]'
29 npm exec [--ws] [-w <workspace-name] [args...]
30
31 npx <pkg>[@<specifier>] [args...]
32 npx -p <pkg>[@<specifier>] <cmd> [args...]
33 npx -c '<cmd> [args...]'
34 npx -p <pkg>[@<specifier>] -c '<cmd> [args...]'
35 Run without --call or positional args to open interactive subshell
36
37 alias: npm x, npx
38
39 common options:
40 --package=<pkg> (may be specified multiple times)
41 -p is a shorthand for --package only when using npx executable
42 -c <cmd> --call=<cmd> (may not be mixed with positional arguments)
43 ```
44
45 ### Description
46
47 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`.
48
49 Run without positional arguments or `--call`, this allows you to interactively run commands in the same sort of shell environment that `package.json` scripts are run. Interactive mode is not supported in CI environments when standard input is a TTY, to prevent hangs.
50
51 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.
52
53 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`).
54
55 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.
56
57 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:
58
59 - 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.
60 - 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.
61 - 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.
62
63 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.
64
65 ### `npx` vs `npm exec`
66
67 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.
68
69 For example:
70
71 ```
72 $ npx foo@latest bar --package=@npmcli/foo
73 ```
74
75 In this case, npm will resolve the `foo` package name, and run the following command:
76
77 ```
78 $ foo bar --package=@npmcli/foo
79 ```
80
81 Since the `--package` option comes _after_ the positional arguments, it is treated as an argument to the executed command.
82
83 In contrast, due to npm's argument parsing logic, running this command is different:
84
85 ```
86 $ npm exec foo@latest bar --package=@npmcli/foo
87 ```
88
89 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:
90
91 ```
92 $ foo@latest bar
93 ```
94
95 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:
96
97 ```
98 $ npm exec -- foo@latest bar --package=@npmcli/foo
99 ```
100
101 ### Configuration
102
103 #### `package`
104
105 - Default:
106 - Type: String (can be set multiple times)
107
108 The package to install for [`npm exec`](/cli/v7/commands/npm-exec)
109
110 #### `call`
111
112 - Default: ""
113 - Type: String
114
115 Optional companion option for `npm exec`, `npx` that allows for specifying a custom command to be run along with the installed packages.
116
117 ```bash
118 npm exec --package yo --package generator-node --call "yo node"
119 ```
120
121 #### `workspace`
122
123 - Default:
124 - Type: String (can be set multiple times)
125
126 Enable running a command in the context of the configured workspaces of the current project while filtering by running only the workspaces defined by this configuration option.
127
128 Valid values for the `workspace` config are either:
129
130 - Workspace names
131 - Path to a workspace directory
132 - Path to a parent workspace directory (will result to selecting all of the nested workspaces)
133
134 When set for the `npm init` command, this may be set to the folder of a workspace which does not yet exist, to create the folder and set it up as a brand new workspace within the project.
135
136 This value is not exported to the environment for child processes.
137
138 #### `workspaces`
139
140 - Default: false
141 - Type: Boolean
142
143 Enable running a command in the context of **all** the configured workspaces.
144
145 This value is not exported to the environment for child processes.
146
147 ### Examples
148
149 Run the version of `tap` in the local dependencies, with the provided arguments:
150
151 ```
152 $ npm exec -- tap --bail test/foo.js
153 $ npx tap --bail test/foo.js
154 ```
155
156 Run a command _other than_ the command whose name matches the package name by specifying a `--package` option:
157
158 ```
159 $ npm exec --package=foo -- bar --bar-argument
160 # ~ or ~
161 $ npx --package=foo bar --bar-argument
162 ```
163
164 Run an arbitrary shell script, in the context of the current project:
165
166 ```
167 $ npm x -c 'eslint && say "hooray, lint passed"'
168 $ npx -c 'eslint && say "hooray, lint passed"'
169 ```
170
171 ### Workspaces support
172
173 You may use the `workspace` or `workspaces` configs in order to run an arbitrary command from an npm package (either one installed locally, or fetched remotely) in the context of the specified workspaces. If no positional argument or `--call` option is provided, it will open an interactive subshell in the context of each of these configured workspaces one at a time.
174
175 Given a project with configured workspaces, e.g:
176
177 ```
178 .
179 +-- package.json
180 `-- packages
181 +-- a
182 | `-- package.json
183 +-- b
184 | `-- package.json
185 `-- c
186 `-- package.json
187 ```
188
189 Assuming the workspace configuration is properly set up at the root level `package.json` file. e.g:
190
191 ```
192 {
193 "workspaces": [ "./packages/*" ]
194 }
195 ```
196
197 You can execute an arbitrary command from a package in the context of each of the configured workspaces when using the `workspaces` configuration options, in this example we're using **eslint** to lint any js file found within each workspace folder:
198
199 ```
200 npm exec --ws -- eslint ./*.js
201 ```
202
203 #### Filtering workspaces
204
205 It's also possible to execute a command in a single workspace using the `workspace` config along with a name or directory path:
206
207 ```
208 npm exec --workspace=a -- eslint ./*.js
209 ```
210
211 The `workspace` config can also be specified multiple times in order to run a specific script in the context of multiple workspaces. When defining values for the `workspace` config in the command line, it also possible to use `-w` as a shorthand, e.g:
212
213 ```
214 npm exec -w a -w b -- eslint ./*.js
215 ```
216
217 This last command will run the `eslint` command in both `./packages/a` and `./packages/b` folders.
218
219 ### Compatibility with Older npx Versions
220
221 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.
222
223 This resulted in some shifts in its functionality:
224
225 - Any `npm` config value may be provided.
226 - 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.
227 - The `--no-install` option is deprecated, and will be converted to `--no`.
228 - Shell fallback functionality is removed, as it is not advisable.
229 - 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.
230 - The `--ignore-existing` option is removed. Locally installed bins are always present in the executed process `PATH`.
231 - The `--npm` option is removed. `npx` will always use the `npm` it ships with.
232 - The `--node-arg` and `-n` options are removed.
233 - The `--always-spawn` option is redundant, and thus removed.
234 - The `--shell` option is replaced with `--script-shell`, but maintained in the `npx` executable for backwards compatibility.
235
236 ### A note on caching
237
238 The npm cli utilizes its internal package cache when using the package name specified. You can use the following to change how and when the cli uses this cache. See [`npm cache`](/cli/v7/commands/npm-cache) for more on how the cache works.
239
240 #### prefer-online
241
242 Forces staleness checks for packages, making the cli look for updates immediately even if the package is already in the cache.
243
244 #### prefer-offline
245
246 Bypasses staleness checks for packages. Missing data will still be requested from the server. To force full offline mode, use `offline`.
247
248 #### offline
249
250 Forces full offline mode. Any packages not locally cached will result in an error.
251
252 #### workspace
253
254 - Default:
255 - Type: String (can be set multiple times)
256
257 Enable running a command in the context of the configured workspaces of the current project while filtering by running only the workspaces defined by this configuration option.
258
259 Valid values for the `workspace` config are either:
260
261 - Workspace names
262 - Path to a workspace directory
263 - Path to a parent workspace directory (will result to selecting all of the nested workspaces)
264
265 This value is not exported to the environment for child processes.
266
267 #### workspaces
268
269 - Alias: `--ws`
270 - Type: Boolean
271 - Default: `false`
272
273 Run scripts in the context of all configured workspaces for the current project.
274
275 ### See Also
276
277 - [npm run-script](/cli/v7/commands/npm-run-script)
278 - [npm scripts](/cli/v7/using-npm/scripts)
279 - [npm test](/cli/v7/commands/npm-test)
280 - [npm start](/cli/v7/commands/npm-start)
281 - [npm restart](/cli/v7/commands/npm-restart)
282 - [npm stop](/cli/v7/commands/npm-stop)
283 - [npm config](/cli/v7/commands/npm-config)
284 - [npm workspaces](/cli/v7/using-npm/workspaces)