1---
2title: npm-exec
3section: 1
4description: Run a command from a local or remote npm package
5---
6
7### Synopsis
8
9```bash
10npm exec -- <pkg>[@<version>] [args...]
11npm exec --package=<pkg>[@<version>] -- <cmd> [args...]
12npm exec -c '<cmd> [args...]'
13npm exec --package=foo -c '<cmd> [args...]'
14
15alias: x
16```
17
18### Description
19
20This command allows you to run an arbitrary command from an npm package
21(either one installed locally, or fetched remotely), in a similar context
22as running it via `npm run`.
23
24Run without positional arguments or `--call`, this allows you to
25interactively run commands in the same sort of shell environment that
26`package.json` scripts are run.  Interactive mode is not supported in CI
27environments when standard input is a TTY, to prevent hangs.
28
29Whatever packages are specified by the `--package` option will be
30provided in the `PATH` of the executed command, along with any locally
31installed package executables.  The `--package` option may be
32specified multiple times, to execute the supplied command in an environment
33where all specified packages are available.
34
35If any requested packages are not present in the local project
36dependencies, then a prompt is printed, which can be suppressed by
37providing either `--yes` or `--no`. When standard input is not a TTY or a
38CI environment is detected, `--yes` is assumed. The requested packages are
39installed to a folder in the npm cache, which is added to the `PATH`
40environment variable in the executed process.
41
42Package names provided without a specifier will be matched with whatever
43version exists in the local project.  Package names with a specifier will
44only be considered a match if they have the exact same name and version as
45the local dependency.
46
47If no `-c` or `--call` option is provided, then the positional arguments
48are used to generate the command string.  If no `--package` options
49are provided, then npm will attempt to determine the executable name from
50the package specifier provided as the first positional argument according
51to the following heuristic:
52
53- If the package has a single entry in its `bin` field in `package.json`,
54  or if all entries are aliases of the same command, then that command
55  will be used.
56- If the package has multiple `bin` entries, and one of them matches the
57  unscoped portion of the `name` field, then that command will be used.
58- If this does not result in exactly one option (either because there are
59  no bin entries, or none of them match the `name` of the package), then
60  `npm exec` exits with an error.
61
62To run a binary _other than_ the named binary, specify one or more
63`--package` options, which will prevent npm from inferring the package from
64the first command argument.
65
66### `npx` vs `npm exec`
67
68When run via the `npx` binary, all flags and options *must* be set prior to
69any positional arguments.  When run via `npm exec`, a double-hyphen `--`
70flag can be used to suppress npm's parsing of switches and options that
71should be sent to the executed command.
72
73For example:
74
75```
76$ npx foo@latest bar --package=@npmcli/foo
77```
78
79In this case, npm will resolve the `foo` package name, and run the
80following command:
81
82```
83$ foo bar --package=@npmcli/foo
84```
85
86Since the `--package` option comes _after_ the positional arguments, it is
87treated as an argument to the executed command.
88
89In contrast, due to npm's argument parsing logic, running this command is
90different:
91
92```
93$ npm exec foo@latest bar --package=@npmcli/foo
94```
95
96In this case, npm will parse the `--package` option first, resolving the
97`@npmcli/foo` package.  Then, it will execute the following command in that
98context:
99
100```
101$ foo@latest bar
102```
103
104The double-hyphen character is recommended to explicitly tell npm to stop
105parsing command line options and switches.  The following command would
106thus be equivalent to the `npx` command above:
107
108```
109$ npm exec -- foo@latest bar --package=@npmcli/foo
110```
111
112### Configuration
113
114#### `package`
115
116* Default:
117* Type: String (can be set multiple times)
118
119The package or packages to install for [`npm exec`](/commands/npm-exec)
120
121
122
123#### `call`
124
125* Default: ""
126* Type: String
127
128Optional companion option for `npm exec`, `npx` that allows for specifying a
129custom command to be run along with the installed packages.
130
131```bash
132npm exec --package yo --package generator-node --call "yo node"
133```
134
135
136
137#### `workspace`
138
139* Default:
140* Type: String (can be set multiple times)
141
142Enable running a command in the context of the configured workspaces of the
143current project while filtering by running only the workspaces defined by
144this configuration option.
145
146Valid values for the `workspace` config are either:
147
148* Workspace names
149* Path to a workspace directory
150* Path to a parent workspace directory (will result in selecting all
151  workspaces within that folder)
152
153When set for the `npm init` command, this may be set to the folder of a
154workspace which does not yet exist, to create the folder and set it up as a
155brand new workspace within the project.
156
157This value is not exported to the environment for child processes.
158
159#### `workspaces`
160
161* Default: null
162* Type: null or Boolean
163
164Set to true to run the command in the context of **all** configured
165workspaces.
166
167Explicitly setting this to false will cause commands like `install` to
168ignore workspaces altogether. When not set explicitly:
169
170- Commands that operate on the `node_modules` tree (install, update, etc.)
171will link workspaces into the `node_modules` folder. - Commands that do
172other things (test, exec, publish, etc.) will operate on the root project,
173_unless_ one or more workspaces are specified in the `workspace` config.
174
175This value is not exported to the environment for child processes.
176
177#### `include-workspace-root`
178
179* Default: false
180* Type: Boolean
181
182Include the workspace root when workspaces are enabled for a command.
183
184When false, specifying individual workspaces via the `workspace` config, or
185all workspaces via the `workspaces` flag, will cause npm to operate only on
186the specified workspaces, and not on the root project.
187
188This value is not exported to the environment for child processes.
189
190### Examples
191
192Run the version of `tap` in the local dependencies, with the provided
193arguments:
194
195```
196$ npm exec -- tap --bail test/foo.js
197$ npx tap --bail test/foo.js
198```
199
200Run a command _other than_ the command whose name matches the package name
201by specifying a `--package` option:
202
203```
204$ npm exec --package=foo -- bar --bar-argument
205# ~ or ~
206$ npx --package=foo bar --bar-argument
207```
208
209Run an arbitrary shell script, in the context of the current project:
210
211```
212$ npm x -c 'eslint && say "hooray, lint passed"'
213$ npx -c 'eslint && say "hooray, lint passed"'
214```
215
216### Workspaces support
217
218You may use the [`workspace`](/using-npm/config#workspace) or
219[`workspaces`](/using-npm/config#workspaces) configs in order to run an
220arbitrary command from an npm package (either one installed locally, or fetched
221remotely) in the context of the specified workspaces.
222If no positional argument or `--call` option is provided, it will open an
223interactive subshell in the context of each of these configured workspaces one
224at a time.
225
226Given a project with configured workspaces, e.g:
227
228```
229.
230+-- package.json
231`-- packages
232   +-- a
233   |   `-- package.json
234   +-- b
235   |   `-- package.json
236   `-- c
237       `-- package.json
238```
239
240Assuming the workspace configuration is properly set up at the root level
241`package.json` file. e.g:
242
243```
244{
245    "workspaces": [ "./packages/*" ]
246}
247```
248
249You can execute an arbitrary command from a package in the context of each of
250the configured workspaces when using the
251[`workspaces` config options](/using-npm/config#workspace), in this example
252we're using **eslint** to lint any js file found within each workspace folder:
253
254```
255npm exec --ws -- eslint ./*.js
256```
257
258#### Filtering workspaces
259
260It's also possible to execute a command in a single workspace using the
261`workspace` config along with a name or directory path:
262
263```
264npm exec --workspace=a -- eslint ./*.js
265```
266
267The `workspace` config can also be specified multiple times in order to run a
268specific script in the context of multiple workspaces. When defining values for
269the `workspace` config in the command line, it also possible to use `-w` as a
270shorthand, e.g:
271
272```
273npm exec -w a -w b -- eslint ./*.js
274```
275
276This last command will run the `eslint` command in both `./packages/a` and
277`./packages/b` folders.
278
279### Compatibility with Older npx Versions
280
281The `npx` binary was rewritten in npm v7.0.0, and the standalone `npx`
282package deprecated at that time.  `npx` uses the `npm exec`
283command instead of a separate argument parser and install process, with
284some affordances to maintain backwards compatibility with the arguments it
285accepted in previous versions.
286
287This resulted in some shifts in its functionality:
288
289- Any `npm` config value may be provided.
290- To prevent security and user-experience problems from mistyping package
291  names, `npx` prompts before installing anything.  Suppress this
292  prompt with the `-y` or `--yes` option.
293- The `--no-install` option is deprecated, and will be converted to `--no`.
294- Shell fallback functionality is removed, as it is not advisable.
295- The `-p` argument is a shorthand for `--parseable` in npm, but shorthand
296  for `--package` in npx.  This is maintained, but only for the `npx`
297  executable.
298- The `--ignore-existing` option is removed.  Locally installed bins are
299  always present in the executed process `PATH`.
300- The `--npm` option is removed.  `npx` will always use the `npm` it ships
301  with.
302- The `--node-arg` and `-n` options are removed.
303- The `--always-spawn` option is redundant, and thus removed.
304- The `--shell` option is replaced with `--script-shell`, but maintained
305  in the `npx` executable for backwards compatibility.
306
307### A note on caching
308
309The npm cli utilizes its internal package cache when using the package
310name specified.  You can use the following to change how and when the
311cli uses this cache. See [`npm cache`](/commands/npm-cache) for more on
312how the cache works.
313
314#### prefer-online
315
316Forces staleness checks for packages, making the cli look for updates
317immediately even if the package is already in the cache.
318
319#### prefer-offline
320
321Bypasses staleness checks for packages.  Missing data will still be
322requested from the server. To force full offline mode, use `offline`.
323
324#### offline
325
326Forces full offline mode. Any packages not locally cached will result in
327an error.
328
329#### workspace
330
331* Default:
332* Type: String (can be set multiple times)
333
334Enable running a command in the context of the configured workspaces of the
335current project while filtering by running only the workspaces defined by
336this configuration option.
337
338Valid values for the `workspace` config are either:
339
340* Workspace names
341* Path to a workspace directory
342* Path to a parent workspace directory (will result to selecting all of the
343  nested workspaces)
344
345This value is not exported to the environment for child processes.
346
347#### workspaces
348
349* Alias: `--ws`
350* Type: Boolean
351* Default: `false`
352
353Run scripts in the context of all configured workspaces for the current
354project.
355
356### See Also
357
358* [npm run-script](/commands/npm-run-script)
359* [npm scripts](/using-npm/scripts)
360* [npm test](/commands/npm-test)
361* [npm start](/commands/npm-start)
362* [npm restart](/commands/npm-restart)
363* [npm stop](/commands/npm-stop)
364* [npm config](/commands/npm-config)
365* [npm workspaces](/using-npm/workspaces)
366* [npx](/commands/npx)
367