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