1NPM-EXEC(1)                                                        NPM-EXEC(1)
2
3
4

NAME

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 to selecting all of
158         the nested workspaces)
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: false
172
173       • Type: Boolean
174
175
176       Enable  running  a  command  in  the  context  of  all  the  configured
177       workspaces.
178
179       This  value  is  not  exported  to the environment for child processes.
180       <!-- automatically  generated,  do  not  edit  manually  -->  <!--  see
181       lib/utils/config/definitions.js -->
182
183       <!-- AUTOGENERATED CONFIG DESCRIPTIONS END -->
184
185
186   Examples
187       Run the version of tap in the local dependencies, with the provided ar‐
188       guments:
189
190         $ npm exec -- tap --bail test/foo.js
191         $ npx tap --bail test/foo.js
192
193       Run a command other than the command whose  name  matches  the  package
194       name by specifying a --package option:
195
196         $ npm exec --package=foo -- bar --bar-argument
197         # ~ or ~
198         $ npx --package=foo bar --bar-argument
199
200       Run an arbitrary shell script, in the context of the current project:
201
202         $ npm x -c 'eslint && say "hooray, lint passed"'
203         $ npx -c 'eslint && say "hooray, lint passed"'
204
205   Workspaces support
206       You  may use the workspace or workspaces configs in order to run an ar‐
207       bitrary command from an npm package (either one installed  locally,  or
208       fetched  remotely)  in  the context of the specified workspaces.  If no
209       positional argument or --call option is provided, it will open  an  in‐
210       teractive   subshell  in  the  context  of  each  of  these  configured
211       workspaces one at a time.
212
213       Given a project with configured workspaces, e.g:
214
215         .
216         +-- package.json
217         `-- packages
218            +-- a
219            |   `-- package.json
220            +-- b
221            |   `-- package.json
222            `-- c
223                `-- package.json
224
225       Assuming the workspace configuration is properly set  up  at  the  root
226       level package.json file. e.g:
227
228         {
229             "workspaces": [ "./packages/*" ]
230         }
231
232       You  can  execute an arbitrary command from a package in the context of
233       each of the configured workspaces when using the workspaces  configura‐
234       tion  options,  in  this example we're using eslint to lint any js file
235       found within each workspace folder:
236
237         npm exec --ws -- eslint ./*.js
238
239   Filtering workspaces
240       It's also possible to execute a command in a single workspace using the
241       workspace config along with a name or directory path:
242
243         npm exec --workspace=a -- eslint ./*.js
244
245       The  workspace  config can also be specified multiple times in order to
246       run a specific script in  the  context  of  multiple  workspaces.  When
247       defining  values  for the workspace config in the command line, it also
248       possible to use -w as a shorthand, e.g:
249
250         npm exec -w a -w b -- eslint ./*.js
251
252       This last command will run the eslint command in both ./packages/a  and
253       ./packages/b folders.
254
255   Compatibility with Older npx Versions
256       The  npx  binary  was  rewritten  in npm v7.0.0, and the standalone npx
257       package deprecated at that time.  npx uses the npm exec command instead
258       of  a  separate  argument  parser and install process, with some affor‐
259       dances to maintain backwards compatibility with the  arguments  it  ac‐
260       cepted in previous versions.
261
262       This resulted in some shifts in its functionality:
263
264       • Any npm config value may be provided.
265
266       • To prevent security and user-experience problems from mistyping pack‐
267         age names, npx prompts before  installing  anything.   Suppress  this
268         prompt with the -y or --yes option.
269
270       • The --no-install option is deprecated, and will be converted to --no.
271
272       • Shell fallback functionality is removed, as it is not advisable.
273
274       • The  -p argument is a shorthand for --parseable in npm, but shorthand
275         for --package in npx.  This is maintained, but only for the npx  exe‐
276         cutable.
277
278       • The  --ignore-existing option is removed.  Locally installed bins are
279         always present in the executed process PATH.
280
281       • The --npm option is removed.  npx will always use the  npm  it  ships
282         with.
283
284       • The --node-arg and -n options are removed.
285
286       • The --always-spawn option is redundant, and thus removed.
287
288       • The --shell option is replaced with --script-shell, but maintained in
289         the npx executable for backwards compatibility.
290
291
292   A note on caching
293       The npm cli utilizes its internal package cache when using the  package
294       name  specified.   You can use the following to change how and when the
295       cli uses this cache. See npm help cache  for  more  on  how  the  cache
296       works.
297
298   prefer-online
299       Forces  staleness  checks for packages, making the cli look for updates
300       immediately even if the package is already in the cache.
301
302   prefer-offline
303       Bypasses staleness checks for packages.  Missing data will still be re‐
304       quested from the server. To force full offline mode, use offline.
305
306   offline
307       Forces  full  offline mode. Any packages not locally cached will result
308       in an error.
309
310   workspace
311       • Default:
312
313       • Type: String (can be set multiple times)
314
315
316       Enable running a command in the context of the configured workspaces of
317       the  current project while filtering by running only the workspaces de‐
318       fined by this configuration option.
319
320       Valid values for the workspace config are either:
321
322       • Workspace names
323
324       • Path to a workspace directory
325
326       • Path to a parent workspace directory (will result to selecting all of
327         the nested workspaces)
328
329
330       This value is not exported to the environment for child processes.
331
332   workspaces
333       • Alias: --ws
334
335       • Type: Boolean
336
337       • Default: false
338
339
340       Run scripts in the context of all configured workspaces for the current
341       project.
342
343   See Also
344       • npm help run-script
345
346       • npm help scripts
347
348       • npm help test
349
350       • npm help start
351
352       • npm help restart
353
354       • npm help stop
355
356       • npm help config
357
358       • npm help workspaces
359
360
361
362
363                                 October 2021                      NPM-EXEC(1)
Impressum