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