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/v8
7 github_path: docs/lib/content/commands/npm-exec.md
8 redirect_from:
9 - /cli-documentation/v8/cli-commands/exec
10 - /cli-documentation/v8/cli-commands/npm-exec
11 - /cli-documentation/v8/commands/exec
12 - /cli-documentation/v8/commands/npm-exec
13 - /cli-documentation/v8/exec
14 - /cli-documentation/v8/npm-exec
15 - /cli/v8/cli-commands/exec
16 - /cli/v8/cli-commands/npm-exec
17 - /cli/v8/commands/exec
18 - /cli/v8/exec
19 - /cli/v8/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
30 alias: x
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 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.
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 ### Configuration
90
91 #### `package`
92
93 - Default:
94 - Type: String (can be set multiple times)
95
96 The package or packages to install for [`npm exec`](/cli/v8/commands/npm-exec)
97
98 #### `call`
99
100 - Default: ""
101 - Type: String
102
103 Optional companion option for `npm exec`, `npx` that allows for specifying a custom command to be run along with the installed packages.
104
105 ```bash
106 npm exec --package yo --package generator-node --call "yo node"
107 ```
108
109 #### `workspace`
110
111 - Default:
112 - Type: String (can be set multiple times)
113
114 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.
115
116 Valid values for the `workspace` config are either:
117
118 - Workspace names
119 - Path to a workspace directory
120 - Path to a parent workspace directory (will result in selecting all workspaces within that folder)
121
122 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.
123
124 This value is not exported to the environment for child processes.
125
126 #### `workspaces`
127
128 - Default: null
129 - Type: null or Boolean
130
131 Set to true to run the command in the context of **all** configured workspaces.
132
133 Explicitly setting this to false will cause commands like `install` to ignore workspaces altogether. When not set explicitly:
134
135 - 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.
136
137 This value is not exported to the environment for child processes.
138
139 #### `include-workspace-root`
140
141 - Default: false
142 - Type: Boolean
143
144 Include the workspace root when workspaces are enabled for a command.
145
146 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.
147
148 This value is not exported to the environment for child processes.
149
150 ### Examples
151
152 Run the version of `tap` in the local dependencies, with the provided arguments:
153
154 ```
155 $ npm exec -- tap --bail test/foo.js
156 $ npx tap --bail test/foo.js
157 ```
158
159 Run a command _other than_ the command whose name matches the package name by specifying a `--package` option:
160
161 ```
162 $ npm exec --package=foo -- bar --bar-argument
163 # ~ or ~
164 $ npx --package=foo bar --bar-argument
165 ```
166
167 Run an arbitrary shell script, in the context of the current project:
168
169 ```
170 $ npm x -c 'eslint && say "hooray, lint passed"'
171 $ npx -c 'eslint && say "hooray, lint passed"'
172 ```
173
174 ### Workspaces support
175
176 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.
177
178 Given a project with configured workspaces, e.g:
179
180 ```
181 .
182 +-- package.json
183 `-- packages
184 +-- a
185 | `-- package.json
186 +-- b
187 | `-- package.json
188 `-- c
189 `-- package.json
190 ```
191
192 Assuming the workspace configuration is properly set up at the root level `package.json` file. e.g:
193
194 ```
195 {
196 "workspaces": [ "./packages/*" ]
197 }
198 ```
199
200 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:
201
202 ```
203 npm exec --ws -- eslint ./*.js
204 ```
205
206 #### Filtering workspaces
207
208 It's also possible to execute a command in a single workspace using the `workspace` config along with a name or directory path:
209
210 ```
211 npm exec --workspace=a -- eslint ./*.js
212 ```
213
214 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:
215
216 ```
217 npm exec -w a -w b -- eslint ./*.js
218 ```
219
220 This last command will run the `eslint` command in both `./packages/a` and `./packages/b` folders.
221
222 ### Compatibility with Older npx Versions
223
224 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.
225
226 This resulted in some shifts in its functionality:
227
228 - Any `npm` config value may be provided.
229 - 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.
230 - The `--no-install` option is deprecated, and will be converted to `--no`.
231 - Shell fallback functionality is removed, as it is not advisable.
232 - 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.
233 - The `--ignore-existing` option is removed. Locally installed bins are always present in the executed process `PATH`.
234 - The `--npm` option is removed. `npx` will always use the `npm` it ships with.
235 - The `--node-arg` and `-n` options are removed.
236 - The `--always-spawn` option is redundant, and thus removed.
237 - The `--shell` option is replaced with `--script-shell`, but maintained in the `npx` executable for backwards compatibility.
238
239 ### A note on caching
240
241 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/v8/commands/npm-cache) for more on how the cache works.
242
243 #### prefer-online
244
245 Forces staleness checks for packages, making the cli look for updates immediately even if the package is already in the cache.
246
247 #### prefer-offline
248
249 Bypasses staleness checks for packages. Missing data will still be requested from the server. To force full offline mode, use `offline`.
250
251 #### offline
252
253 Forces full offline mode. Any packages not locally cached will result in an error.
254
255 #### workspace
256
257 - Default:
258 - Type: String (can be set multiple times)
259
260 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.
261
262 Valid values for the `workspace` config are either:
263
264 - Workspace names
265 - Path to a workspace directory
266 - Path to a parent workspace directory (will result to selecting all of the nested workspaces)
267
268 This value is not exported to the environment for child processes.
269
270 #### workspaces
271
272 - Alias: `--ws`
273 - Type: Boolean
274 - Default: `false`
275
276 Run scripts in the context of all configured workspaces for the current project.
277
278 ### See Also
279
280 - [npm run-script](/cli/v8/commands/npm-run-script)
281 - [npm scripts](/cli/v8/using-npm/scripts)
282 - [npm test](/cli/v8/commands/npm-test)
283 - [npm start](/cli/v8/commands/npm-start)
284 - [npm restart](/cli/v8/commands/npm-restart)
285 - [npm stop](/cli/v8/commands/npm-stop)
286 - [npm config](/cli/v8/commands/npm-config)
287 - [npm workspaces](/cli/v8/using-npm/workspaces)
288 - [npx](/cli/v8/commands/npx)