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