版博士V2.0程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. <picture>
  2. <source media="(prefers-color-scheme: dark)" srcset="media/logo_dark.svg">
  3. <img alt="execa logo" src="media/logo.svg" width="400">
  4. </picture>
  5. <br>
  6. [![Coverage Status](https://codecov.io/gh/sindresorhus/execa/branch/main/graph/badge.svg)](https://codecov.io/gh/sindresorhus/execa)
  7. > Process execution for humans
  8. ## Why
  9. This package improves [`child_process`](https://nodejs.org/api/child_process.html) methods with:
  10. - [Promise interface](#execacommandcommand-options).
  11. - [Scripts interface](#scripts-interface), like `zx`.
  12. - Improved [Windows support](https://github.com/IndigoUnited/node-cross-spawn#why), including [shebang](https://en.wikipedia.org/wiki/Shebang_(Unix)) binaries.
  13. - Executes [locally installed binaries](#preferlocal) without `npx`.
  14. - [Cleans up](#cleanup) child processes when the parent process ends.
  15. - [Graceful termination](#optionsforcekillaftertimeout).
  16. - Get [interleaved output](#all) from `stdout` and `stderr` similar to what is printed on the terminal.
  17. - [Strips the final newline](#stripfinalnewline) from the output so you don't have to do `stdout.trim()`.
  18. - Convenience methods to pipe processes' [input](#input) and [output](#redirect-output-to-a-file).
  19. - Can specify file and arguments [as a single string](#execacommandcommand-options) without a shell.
  20. - [Verbose mode](#verbose-mode) for debugging.
  21. - More descriptive errors.
  22. - Higher max buffer: 100 MB instead of 1 MB.
  23. ## Install
  24. ```sh
  25. npm install execa
  26. ```
  27. ## Usage
  28. ### Promise interface
  29. ```js
  30. import {execa} from 'execa';
  31. const {stdout} = await execa('echo', ['unicorns']);
  32. console.log(stdout);
  33. //=> 'unicorns'
  34. ```
  35. ### Scripts interface
  36. For more information about Execa scripts, please see [this page](docs/scripts.md).
  37. #### Basic
  38. ```js
  39. import {$} from 'execa';
  40. const branch = await $`git branch --show-current`;
  41. await $`dep deploy --branch=${branch}`;
  42. ```
  43. #### Multiple arguments
  44. ```js
  45. import {$} from 'execa';
  46. const args = ['unicorns', '&', 'rainbows!'];
  47. const {stdout} = await $`echo ${args}`;
  48. console.log(stdout);
  49. //=> 'unicorns & rainbows!'
  50. ```
  51. #### With options
  52. ```js
  53. import {$} from 'execa';
  54. await $({stdio: 'inherit'})`echo unicorns`;
  55. //=> 'unicorns'
  56. ```
  57. #### Shared options
  58. ```js
  59. import {$} from 'execa';
  60. const $$ = $({stdio: 'inherit'});
  61. await $$`echo unicorns`;
  62. //=> 'unicorns'
  63. await $$`echo rainbows`;
  64. //=> 'rainbows'
  65. ```
  66. #### Verbose mode
  67. ```sh
  68. > node file.js
  69. unicorns
  70. rainbows
  71. > NODE_DEBUG=execa node file.js
  72. [16:50:03.305] echo unicorns
  73. unicorns
  74. [16:50:03.308] echo rainbows
  75. rainbows
  76. ```
  77. ### Input/output
  78. #### Redirect output to a file
  79. ```js
  80. import {execa} from 'execa';
  81. // Similar to `echo unicorns > stdout.txt` in Bash
  82. await execa('echo', ['unicorns']).pipeStdout('stdout.txt');
  83. // Similar to `echo unicorns 2> stdout.txt` in Bash
  84. await execa('echo', ['unicorns']).pipeStderr('stderr.txt');
  85. // Similar to `echo unicorns &> stdout.txt` in Bash
  86. await execa('echo', ['unicorns'], {all: true}).pipeAll('all.txt');
  87. ```
  88. #### Redirect input from a file
  89. ```js
  90. import {execa} from 'execa';
  91. // Similar to `cat < stdin.txt` in Bash
  92. const {stdout} = await execa('cat', {inputFile: 'stdin.txt'});
  93. console.log(stdout);
  94. //=> 'unicorns'
  95. ```
  96. #### Save and pipe output from a child process
  97. ```js
  98. import {execa} from 'execa';
  99. const {stdout} = await execa('echo', ['unicorns']).pipeStdout(process.stdout);
  100. // Prints `unicorns`
  101. console.log(stdout);
  102. // Also returns 'unicorns'
  103. ```
  104. #### Pipe multiple processes
  105. ```js
  106. import {execa} from 'execa';
  107. // Similar to `echo unicorns | cat` in Bash
  108. const {stdout} = await execa('echo', ['unicorns']).pipeStdout(execa('cat'));
  109. console.log(stdout);
  110. //=> 'unicorns'
  111. ```
  112. ### Handling Errors
  113. ```js
  114. import {execa} from 'execa';
  115. // Catching an error
  116. try {
  117. await execa('unknown', ['command']);
  118. } catch (error) {
  119. console.log(error);
  120. /*
  121. {
  122. message: 'Command failed with ENOENT: unknown command spawn unknown ENOENT',
  123. errno: -2,
  124. code: 'ENOENT',
  125. syscall: 'spawn unknown',
  126. path: 'unknown',
  127. spawnargs: ['command'],
  128. originalMessage: 'spawn unknown ENOENT',
  129. shortMessage: 'Command failed with ENOENT: unknown command spawn unknown ENOENT',
  130. command: 'unknown command',
  131. escapedCommand: 'unknown command',
  132. stdout: '',
  133. stderr: '',
  134. failed: true,
  135. timedOut: false,
  136. isCanceled: false,
  137. killed: false
  138. }
  139. */
  140. }
  141. ```
  142. ### Graceful termination
  143. Using SIGTERM, and after 2 seconds, kill it with SIGKILL.
  144. ```js
  145. const subprocess = execa('node');
  146. setTimeout(() => {
  147. subprocess.kill('SIGTERM', {
  148. forceKillAfterTimeout: 2000
  149. });
  150. }, 1000);
  151. ```
  152. ## API
  153. ### Methods
  154. #### execa(file, arguments?, options?)
  155. Executes a command using `file ...arguments`. `arguments` are specified as an array of strings. Returns a [`childProcess`](#childprocess).
  156. Arguments are [automatically escaped](#shell-syntax). They can contain any character, including spaces.
  157. This is the preferred method when executing single commands.
  158. #### execaNode(scriptPath, arguments?, options?)
  159. Executes a Node.js file using `node scriptPath ...arguments`. `arguments` are specified as an array of strings. Returns a [`childProcess`](#childprocess).
  160. Arguments are [automatically escaped](#shell-syntax). They can contain any character, including spaces.
  161. This is the preferred method when executing Node.js files.
  162. Like [`child_process#fork()`](https://nodejs.org/api/child_process.html#child_process_child_process_fork_modulepath_args_options):
  163. - the current Node version and options are used. This can be overridden using the [`nodePath`](#nodepath-for-node-only) and [`nodeOptions`](#nodeoptions-for-node-only) options.
  164. - the [`shell`](#shell) option cannot be used
  165. - an extra channel [`ipc`](https://nodejs.org/api/child_process.html#child_process_options_stdio) is passed to [`stdio`](#stdio)
  166. #### $\`command\`
  167. Executes a command. The `command` string includes both the `file` and its `arguments`. Returns a [`childProcess`](#childprocess).
  168. Arguments are [automatically escaped](#shell-syntax). They can contain any character, but spaces must use `${}` like `` $`echo ${'has space'}` ``.
  169. This is the preferred method when executing multiple commands in a script file.
  170. The `command` string can inject any `${value}` with the following types: string, number, [`childProcess`](#childprocess) or an array of those types. For example: `` $`echo one ${'two'} ${3} ${['four', 'five']}` ``. For `${childProcess}`, the process's `stdout` is used.
  171. For more information, please see [this section](#scripts-interface) and [this page](docs/scripts.md).
  172. #### $(options)
  173. Returns a new instance of [`$`](#command) but with different default `options`. Consecutive calls are merged to previous ones.
  174. This can be used to either:
  175. - Set options for a specific command: `` $(options)`command` ``
  176. - Share options for multiple commands: `` const $$ = $(options); $$`command`; $$`otherCommand`; ``
  177. #### execaCommand(command, options?)
  178. Executes a command. The `command` string includes both the `file` and its `arguments`. Returns a [`childProcess`](#childprocess).
  179. Arguments are [automatically escaped](#shell-syntax). They can contain any character, but spaces must be escaped with a backslash like `execaCommand('echo has\\ space')`.
  180. This is the preferred method when executing a user-supplied `command` string, such as in a REPL.
  181. ### execaSync(file, arguments?, options?)
  182. Same as [`execa()`](#execacommandcommand-options) but synchronous.
  183. Returns or throws a [`childProcessResult`](#childProcessResult).
  184. ### $.sync\`command\`
  185. Same as [$\`command\`](#command) but synchronous.
  186. Returns or throws a [`childProcessResult`](#childProcessResult).
  187. ### execaCommandSync(command, options?)
  188. Same as [`execaCommand()`](#execacommand-command-options) but synchronous.
  189. Returns or throws a [`childProcessResult`](#childProcessResult).
  190. ### Shell syntax
  191. For all the [methods above](#methods), no shell interpreter (Bash, cmd.exe, etc.) is used unless the [`shell` option](#shell) is set. This means shell-specific characters and expressions (`$variable`, `&&`, `||`, `;`, `|`, etc.) have no special meaning and do not need to be escaped.
  192. ### childProcess
  193. The return value of all [asynchronous methods](#methods) is both:
  194. - a `Promise` resolving or rejecting with a [`childProcessResult`](#childProcessResult).
  195. - a [`child_process` instance](https://nodejs.org/api/child_process.html#child_process_class_childprocess) with the following additional methods and properties.
  196. #### kill(signal?, options?)
  197. Same as the original [`child_process#kill()`](https://nodejs.org/api/child_process.html#child_process_subprocess_kill_signal) except: if `signal` is `SIGTERM` (the default value) and the child process is not terminated after 5 seconds, force it by sending `SIGKILL`.
  198. ##### options.forceKillAfterTimeout
  199. Type: `number | false`\
  200. Default: `5000`
  201. Milliseconds to wait for the child process to terminate before sending `SIGKILL`.
  202. Can be disabled with `false`.
  203. #### all
  204. Type: `ReadableStream | undefined`
  205. Stream combining/interleaving [`stdout`](https://nodejs.org/api/child_process.html#child_process_subprocess_stdout) and [`stderr`](https://nodejs.org/api/child_process.html#child_process_subprocess_stderr).
  206. This is `undefined` if either:
  207. - the [`all` option](#all-2) is `false` (the default value)
  208. - both [`stdout`](#stdout-1) and [`stderr`](#stderr-1) options are set to [`'inherit'`, `'ipc'`, `Stream` or `integer`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio)
  209. #### pipeStdout(target)
  210. [Pipe](https://nodejs.org/api/stream.html#readablepipedestination-options) the child process's `stdout` to `target`, which can be:
  211. - Another [`execa()` return value](#pipe-multiple-processes)
  212. - A [writable stream](#save-and-pipe-output-from-a-child-process)
  213. - A [file path string](#redirect-output-to-a-file)
  214. If the `target` is another [`execa()` return value](#execacommandcommand-options), it is returned. Otherwise, the original `execa()` return value is returned. This allows chaining `pipeStdout()` then `await`ing the [final result](#childprocessresult).
  215. The [`stdout` option](#stdout-1) must be kept as `pipe`, its default value.
  216. #### pipeStderr(target)
  217. Like [`pipeStdout()`](#pipestdouttarget) but piping the child process's `stderr` instead.
  218. The [`stderr` option](#stderr-1) must be kept as `pipe`, its default value.
  219. #### pipeAll(target)
  220. Combines both [`pipeStdout()`](#pipestdouttarget) and [`pipeStderr()`](#pipestderrtarget).
  221. Either the [`stdout` option](#stdout-1) or the [`stderr` option](#stderr-1) must be kept as `pipe`, their default value. Also, the [`all` option](#all-2) must be set to `true`.
  222. ### childProcessResult
  223. Type: `object`
  224. Result of a child process execution. On success this is a plain object. On failure this is also an `Error` instance.
  225. The child process [fails](#failed) when:
  226. - its [exit code](#exitcode) is not `0`
  227. - it was [killed](#killed) with a [signal](#signal)
  228. - [timing out](#timedout)
  229. - [being canceled](#iscanceled)
  230. - there's not enough memory or there are already too many child processes
  231. #### command
  232. Type: `string`
  233. The file and arguments that were run, for logging purposes.
  234. This is not escaped and should not be executed directly as a process, including using [`execa()`](#execafile-arguments-options) or [`execaCommand()`](#execacommandcommand-options).
  235. #### escapedCommand
  236. Type: `string`
  237. Same as [`command`](#command-1) but escaped.
  238. This is meant to be copy and pasted into a shell, for debugging purposes.
  239. Since the escaping is fairly basic, this should not be executed directly as a process, including using [`execa()`](#execafile-arguments-options) or [`execaCommand()`](#execacommandcommand-options).
  240. #### exitCode
  241. Type: `number`
  242. The numeric exit code of the process that was run.
  243. #### stdout
  244. Type: `string | Buffer`
  245. The output of the process on stdout.
  246. #### stderr
  247. Type: `string | Buffer`
  248. The output of the process on stderr.
  249. #### all
  250. Type: `string | Buffer | undefined`
  251. The output of the process with `stdout` and `stderr` interleaved.
  252. This is `undefined` if either:
  253. - the [`all` option](#all-2) is `false` (the default value)
  254. - `execaSync()` was used
  255. #### failed
  256. Type: `boolean`
  257. Whether the process failed to run.
  258. #### timedOut
  259. Type: `boolean`
  260. Whether the process timed out.
  261. #### isCanceled
  262. Type: `boolean`
  263. Whether the process was canceled.
  264. You can cancel the spawned process using the [`signal`](#signal-1) option.
  265. #### killed
  266. Type: `boolean`
  267. Whether the process was killed.
  268. #### signal
  269. Type: `string | undefined`
  270. The name of the signal that was used to terminate the process. For example, `SIGFPE`.
  271. If a signal terminated the process, this property is defined and included in the error message. Otherwise it is `undefined`.
  272. #### signalDescription
  273. Type: `string | undefined`
  274. A human-friendly description of the signal that was used to terminate the process. For example, `Floating point arithmetic error`.
  275. If a signal terminated the process, this property is defined and included in the error message. Otherwise it is `undefined`. It is also `undefined` when the signal is very uncommon which should seldomly happen.
  276. #### message
  277. Type: `string`
  278. Error message when the child process failed to run. In addition to the [underlying error message](#originalMessage), it also contains some information related to why the child process errored.
  279. The child process [stderr](#stderr) then [stdout](#stdout) are appended to the end, separated with newlines and not interleaved.
  280. #### shortMessage
  281. Type: `string`
  282. This is the same as the [`message` property](#message) except it does not include the child process stdout/stderr.
  283. #### originalMessage
  284. Type: `string | undefined`
  285. Original error message. This is the same as the `message` property except it includes neither the child process stdout/stderr nor some additional information added by Execa.
  286. This is `undefined` unless the child process exited due to an `error` event or a timeout.
  287. ### options
  288. Type: `object`
  289. #### cleanup
  290. Type: `boolean`\
  291. Default: `true`
  292. Kill the spawned process when the parent process exits unless either:
  293. - the spawned process is [`detached`](https://nodejs.org/api/child_process.html#child_process_options_detached)
  294. - the parent process is terminated abruptly, for example, with `SIGKILL` as opposed to `SIGTERM` or a normal exit
  295. #### preferLocal
  296. Type: `boolean`\
  297. Default: `true` with [`$`](#command), `false` otherwise
  298. Prefer locally installed binaries when looking for a binary to execute.\
  299. If you `$ npm install foo`, you can then `execa('foo')`.
  300. #### localDir
  301. Type: `string | URL`\
  302. Default: `process.cwd()`
  303. Preferred path to find locally installed binaries in (use with `preferLocal`).
  304. #### execPath
  305. Type: `string`\
  306. Default: `process.execPath` (Current Node.js executable)
  307. Path to the Node.js executable to use in child processes.
  308. This can be either an absolute path or a path relative to the [`cwd` option](#cwd).
  309. Requires [`preferLocal`](#preferlocal) to be `true`.
  310. For example, this can be used together with [`get-node`](https://github.com/ehmicky/get-node) to run a specific Node.js version in a child process.
  311. #### buffer
  312. Type: `boolean`\
  313. Default: `true`
  314. Buffer the output from the spawned process. When set to `false`, you must read the output of [`stdout`](#stdout-1) and [`stderr`](#stderr-1) (or [`all`](#all) if the [`all`](#all-2) option is `true`). Otherwise the returned promise will not be resolved/rejected.
  315. If the spawned process fails, [`error.stdout`](#stdout), [`error.stderr`](#stderr), and [`error.all`](#all) will contain the buffered data.
  316. #### input
  317. Type: `string | Buffer | stream.Readable`
  318. Write some input to the `stdin` of your binary.\
  319. Streams are not allowed when using the synchronous methods.
  320. If the input is a file, use the [`inputFile` option](#inputfile) instead.
  321. #### inputFile
  322. Type: `string`
  323. Use a file as input to the the `stdin` of your binary.
  324. If the input is not a file, use the [`input` option](#input) instead.
  325. #### stdin
  326. Type: `string | number | Stream | undefined`\
  327. Default: `inherit` with [`$`](#command), `pipe` otherwise
  328. Same options as [`stdio`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).
  329. #### stdout
  330. Type: `string | number | Stream | undefined`\
  331. Default: `pipe`
  332. Same options as [`stdio`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).
  333. #### stderr
  334. Type: `string | number | Stream | undefined`\
  335. Default: `pipe`
  336. Same options as [`stdio`](https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_options_stdio).
  337. #### all
  338. Type: `boolean`\
  339. Default: `false`
  340. Add an `.all` property on the [promise](#all) and the [resolved value](#all-1). The property contains the output of the process with `stdout` and `stderr` interleaved.
  341. #### reject
  342. Type: `boolean`\
  343. Default: `true`
  344. Setting this to `false` resolves the promise with the error instead of rejecting it.
  345. #### stripFinalNewline
  346. Type: `boolean`\
  347. Default: `true`
  348. Strip the final [newline character](https://en.wikipedia.org/wiki/Newline) from the output.
  349. #### extendEnv
  350. Type: `boolean`\
  351. Default: `true`
  352. Set to `false` if you don't want to extend the environment variables when providing the `env` property.
  353. ---
  354. Execa also accepts the below options which are the same as the options for [`child_process#spawn()`](https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options)/[`child_process#exec()`](https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback)
  355. #### cwd
  356. Type: `string | URL`\
  357. Default: `process.cwd()`
  358. Current working directory of the child process.
  359. #### env
  360. Type: `object`\
  361. Default: `process.env`
  362. Environment key-value pairs. Extends automatically from `process.env`. Set [`extendEnv`](#extendenv) to `false` if you don't want this.
  363. #### argv0
  364. Type: `string`
  365. Explicitly set the value of `argv[0]` sent to the child process. This will be set to `file` if not specified.
  366. #### stdio
  367. Type: `string | string[]`\
  368. Default: `pipe`
  369. Child's [stdio](https://nodejs.org/api/child_process.html#child_process_options_stdio) configuration.
  370. #### serialization
  371. Type: `string`\
  372. Default: `'json'`
  373. Specify the kind of serialization used for sending messages between processes when using the [`stdio: 'ipc'`](#stdio) option or [`execaNode()`](#execanodescriptpath-arguments-options):
  374. - `json`: Uses `JSON.stringify()` and `JSON.parse()`.
  375. - `advanced`: Uses [`v8.serialize()`](https://nodejs.org/api/v8.html#v8_v8_serialize_value)
  376. [More info.](https://nodejs.org/api/child_process.html#child_process_advanced_serialization)
  377. #### detached
  378. Type: `boolean`
  379. Prepare child to run independently of its parent process. Specific behavior [depends on the platform](https://nodejs.org/api/child_process.html#child_process_options_detached).
  380. #### uid
  381. Type: `number`
  382. Sets the user identity of the process.
  383. #### gid
  384. Type: `number`
  385. Sets the group identity of the process.
  386. #### shell
  387. Type: `boolean | string`\
  388. Default: `false`
  389. If `true`, runs `file` inside of a shell. Uses `/bin/sh` on UNIX and `cmd.exe` on Windows. A different shell can be specified as a string. The shell should understand the `-c` switch on UNIX or `/d /s /c` on Windows.
  390. We recommend against using this option since it is:
  391. - not cross-platform, encouraging shell-specific syntax.
  392. - slower, because of the additional shell interpretation.
  393. - unsafe, potentially allowing command injection.
  394. #### encoding
  395. Type: `string | null`\
  396. Default: `utf8`
  397. Specify the character encoding used to decode the `stdout` and `stderr` output. If set to `null`, then `stdout` and `stderr` will be a `Buffer` instead of a string.
  398. #### timeout
  399. Type: `number`\
  400. Default: `0`
  401. If timeout is greater than `0`, the parent will send the signal identified by the `killSignal` property (the default is `SIGTERM`) if the child runs longer than timeout milliseconds.
  402. #### maxBuffer
  403. Type: `number`\
  404. Default: `100_000_000` (100 MB)
  405. Largest amount of data in bytes allowed on `stdout` or `stderr`.
  406. #### killSignal
  407. Type: `string | number`\
  408. Default: `SIGTERM`
  409. Signal value to be used when the spawned process will be killed.
  410. #### signal
  411. Type: [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal)
  412. You can abort the spawned process using [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController).
  413. When `AbortController.abort()` is called, [`.isCanceled`](#iscanceled) becomes `false`.
  414. *Requires Node.js 16 or later.*
  415. #### windowsVerbatimArguments
  416. Type: `boolean`\
  417. Default: `false`
  418. If `true`, no quoting or escaping of arguments is done on Windows. Ignored on other platforms. This is set to `true` automatically when the `shell` option is `true`.
  419. #### windowsHide
  420. Type: `boolean`\
  421. Default: `true`
  422. On Windows, do not create a new console window. Please note this also prevents `CTRL-C` [from working](https://github.com/nodejs/node/issues/29837) on Windows.
  423. #### verbose
  424. Type: `boolean`\
  425. Default: `false`
  426. [Print each command](#verbose-mode) on `stderr` before executing it.
  427. This can also be enabled by setting the `NODE_DEBUG=execa` environment variable in the current process.
  428. #### nodePath *(For `.node()` only)*
  429. Type: `string`\
  430. Default: [`process.execPath`](https://nodejs.org/api/process.html#process_process_execpath)
  431. Node.js executable used to create the child process.
  432. #### nodeOptions *(For `.node()` only)*
  433. Type: `string[]`\
  434. Default: [`process.execArgv`](https://nodejs.org/api/process.html#process_process_execargv)
  435. List of [CLI options](https://nodejs.org/api/cli.html#cli_options) passed to the Node.js executable.
  436. ## Tips
  437. ### Retry on error
  438. Gracefully handle failures by using automatic retries and exponential backoff with the [`p-retry`](https://github.com/sindresorhus/p-retry) package:
  439. ```js
  440. import pRetry from 'p-retry';
  441. const run = async () => {
  442. const results = await execa('curl', ['-sSL', 'https://sindresorhus.com/unicorn']);
  443. return results;
  444. };
  445. console.log(await pRetry(run, {retries: 5}));
  446. ```
  447. ### Cancelling a spawned process
  448. ```js
  449. import {execa} from 'execa';
  450. const abortController = new AbortController();
  451. const subprocess = execa('node', [], {signal: abortController.signal});
  452. setTimeout(() => {
  453. abortController.abort();
  454. }, 1000);
  455. try {
  456. await subprocess;
  457. } catch (error) {
  458. console.log(subprocess.killed); // true
  459. console.log(error.isCanceled); // true
  460. }
  461. ```
  462. ### Execute the current package's binary
  463. ```js
  464. import {getBinPath} from 'get-bin-path';
  465. const binPath = await getBinPath();
  466. await execa(binPath);
  467. ```
  468. `execa` can be combined with [`get-bin-path`](https://github.com/ehmicky/get-bin-path) to test the current package's binary. As opposed to hard-coding the path to the binary, this validates that the `package.json` `bin` field is correctly set up.
  469. ## Related
  470. - [gulp-execa](https://github.com/ehmicky/gulp-execa) - Gulp plugin for `execa`
  471. - [nvexeca](https://github.com/ehmicky/nvexeca) - Run `execa` using any Node.js version
  472. - [sudo-prompt](https://github.com/jorangreef/sudo-prompt) - Run commands with elevated privileges.
  473. ## Maintainers
  474. - [Sindre Sorhus](https://github.com/sindresorhus)
  475. - [@ehmicky](https://github.com/ehmicky)
  476. ---
  477. <div align="center">
  478. <b>
  479. <a href="https://tidelift.com/subscription/pkg/npm-execa?utm_source=npm-execa&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
  480. </b>
  481. <br>
  482. <sub>
  483. Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
  484. </sub>
  485. </div>