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