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/v11/cli-commands/exec
18 - /cli-documentation/v11/cli-commands/npm-exec
19 - /cli-documentation/v11/commands/exec
20 - /cli-documentation/v11/commands/npm-exec
21 - /cli-documentation/v11/exec
22 - /cli-documentation/v11/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/v11/cli-commands/exec
30 - /cli/v11/cli-commands/npm-exec
31 - /cli/v11/commands/exec
32 - /cli/v11/exec
33 - /cli/v11/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/v11/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 #### `allow-scripts`
167
168 - Default: ""
169 - Type: String (can be set multiple times)
170
171 Comma-separated list of packages whose install-time lifecycle scripts (`preinstall`, `install`, `postinstall`, and `prepare` for non-registry dependencies) are allowed to run.
172
173 This setting is intended for one-off and global contexts: `npm exec`, `npx`, and `npm install -g`, where no project `package.json` is involved. For team-wide policy in a project, use the `allowScripts` field in `package.json` (which also supports explicit denials), or configure it in `.npmrc`. Passing `--allow-scripts` on the command line during a project-scoped `npm install`, `ci`, `update`, or `rebuild` is an error.
174
175 Each name is matched against a dependency's resolved identity, not against the package's self-reported name. `--ignore-scripts` and `--dangerously-allow-all-scripts` both override this setting.
176
177 #### `strict-allow-scripts`
178
179 - Default: false
180 - Type: Boolean
181
182 If `true`, turn the install-script policy from a warning into a hard error: any dependency with install scripts not covered by `allowScripts` will fail the install instead of running with a notice.
183
184 Dependencies explicitly denied with `false` in `allowScripts` are always silently skipped; this setting only affects unreviewed entries. `--ignore-scripts` and `--dangerously-allow-all-scripts` both override this setting.
185
186 #### `dangerously-allow-all-scripts`
187
188 - Default: false
189 - Type: Boolean
190
191 If `true`, bypass the `allowScripts` policy entirely and run every dependency install script regardless of whether it was approved or denied. Intended as a migration escape hatch only; its use is strongly discouraged. `--ignore-scripts` still takes precedence over this setting.
192
193 ### Examples
194
195 Run the version of `tap` in the local dependencies, with the provided arguments:
196
197 ```
198 $ npm exec -- tap --bail test/foo.js
199 $ npx tap --bail test/foo.js
200 ```
201
202 Run a command _other than_ the command whose name matches the package name by specifying a `--package` option:
203
204 ```
205 $ npm exec --package=foo -- bar --bar-argument
206 # ~ or ~
207 $ npx --package=foo bar --bar-argument
208 ```
209
210 Run an arbitrary shell script, in the context of the current project:
211
212 ```
213 $ npm x -c 'eslint && say "hooray, lint passed"'
214 $ npx -c 'eslint && say "hooray, lint passed"'
215 ```
216
217 ### Workspaces support
218
219 You may use the [`workspace`](/cli/v11/using-npm/config#workspace) or [`workspaces`](/cli/v11/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.
220
221 Given a project with configured workspaces, e.g:
222
223 ```
224 .
225 +-- package.json
226 `-- packages
227 +-- a
228 | `-- package.json
229 +-- b
230 | `-- package.json
231 `-- c
232 `-- package.json
233 ```
234
235 Assuming the workspace configuration is properly set up at the root level `package.json` file. e.g:
236
237 ```
238 {
239 "workspaces": [ "./packages/*" ]
240 }
241 ```
242
243 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/v11/using-npm/config#workspace), in this example we're using **eslint** to lint any js file found within each workspace folder:
244
245 ```
246 npm exec --ws -- eslint ./*.js
247 ```
248
249 #### Filtering workspaces
250
251 It's also possible to execute a command in a single workspace using the `workspace` config along with a name or directory path:
252
253 ```
254 npm exec --workspace=a -- eslint ./*.js
255 ```
256
257 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:
258
259 ```
260 npm exec -w a -w b -- eslint ./*.js
261 ```
262
263 This last command will run the `eslint` command in both `./packages/a` and `./packages/b` folders.
264
265 ### Compatibility with Older npx Versions
266
267 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.
268
269 This resulted in some shifts in its functionality:
270
271 - Any `npm` config value may be provided.
272 - 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.
273 - The `--no-install` option is deprecated, and will be converted to `--no`.
274 - Shell fallback functionality is removed, as it is not advisable.
275 - 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.
276 - The `--ignore-existing` option is removed. Locally installed bins are always present in the executed process `PATH`.
277 - The `--npm` option is removed. `npx` will always use the `npm` it ships with.
278 - The `--node-arg` and `-n` options are removed.
279 - The `--always-spawn` option is redundant, and thus removed.
280 - The `--shell` option is replaced with `--script-shell`, but maintained in the `npx` executable for backwards compatibility.
281
282 ### A note on caching
283
284 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/v11/commands/npm-cache) for more on how the cache works.
285
286 #### prefer-online
287
288 Forces staleness checks for packages, making the cli look for updates immediately even if the package is already in the cache.
289
290 #### prefer-offline
291
292 Bypasses staleness checks for packages. Missing data will still be requested from the server. To force full offline mode, use `offline`.
293
294 #### offline
295
296 Forces full offline mode. Any packages not locally cached will result in an error.
297
298 #### workspace
299
300 - Default:
301 - Type: String (can be set multiple times)
302
303 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.
304
305 Valid values for the `workspace` config are either:
306
307 - Workspace names
308 - Path to a workspace directory
309 - Path to a parent workspace directory (will result to selecting all of the nested workspaces)
310
311 This value is not exported to the environment for child processes.
312
313 #### workspaces
314
315 - Alias: `--ws`
316 - Type: Boolean
317 - Default: `false`
318
319 Run scripts in the context of all configured workspaces for the current project.
320
321 ### See Also
322
323 - [npm run](/cli/v11/commands/npm-run)
324 - [npm scripts](/cli/v11/using-npm/scripts)
325 - [npm test](/cli/v11/commands/npm-test)
326 - [npm start](/cli/v11/commands/npm-start)
327 - [npm restart](/cli/v11/commands/npm-restart)
328 - [npm stop](/cli/v11/commands/npm-stop)
329 - [npm config](/cli/v11/commands/npm-config)
330 - [npm workspaces](/cli/v11/using-npm/workspaces)
331 - [npx](/cli/v11/commands/npx)