execution.md
This file
Path
docs/execution.md
137 lines · 4.5 KiB · Markdown
Last changed by
Add user guides (#989)ehmickybefore adoptionbe91ecf
History of this fileTake it
Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 1–137why, one step back1<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>67# ▶️ Basic execution89## Array syntax1011```js12import {execa} from 'execa';1314await execa('npm', ['run', 'build']);15```1617## Template string syntax1819All [available methods](../readme.md#methods) can use either the [array syntax](#array-syntax) or the template string syntax, which are equivalent.2021```js22await execa`npm run build`;23```2425### String argument2627```js28await execa`npm run ${'task with space'}`;29```3031### Number argument3233```js34await execa`npm run build --concurrency ${2}`;35```3637### Subcommands3839```js40const result = await execa`get-concurrency`;4142// Uses `result.stdout`43await execa`npm run build --concurrency ${result}`;44```4546### Concatenation4748```js49const tmpDirectory = '/tmp';50await execa`mkdir ${tmpDirectory}/filename`;51```5253### Multiple arguments5455```js56const result = await execa`get-concurrency`;5758await execa`npm ${['run', 'build', '--concurrency', 2]}`;59```6061### Multiple lines6263```js64await execa`npm run build65 --concurrency 266 --fail-fast`;67```6869## Options7071[Options](../readme.md#options) can be passed to influence the execution's behavior.7273### Array syntax7475```js76await execa('npm', ['run', 'build'], {timeout: 5000});77```7879### Template string syntax8081```js82await execa({timeout: 5000})`npm run build`;83```8485### Global/shared options8687```js88const timedExeca = execa({timeout: 5000});8990await timedExeca('npm', ['run', 'build']);91await timedExeca`npm run test`;92```9394## Return value9596### Subprocess9798The subprocess is returned as soon as it is spawned. It is a [`child_process` instance](https://nodejs.org/api/child_process.html#child_process_class_childprocess) with [additional methods and properties](../readme.md#subprocess).99100```js101const subprocess = execa`npm run build`;102console.log(subprocess.pid);103```104105### Result106107The subprocess is also a `Promise` that resolves with the [`result`](../readme.md#result).108109```js110const {stdout} = await execa`npm run build`;111```112113### Synchronous execution114115[Every method](../readme.md#methods) can be called synchronously by appending `Sync` to the method's name. The [`result`](../readme.md#result) is returned without needing to `await`. The [`subprocess`](#subprocess) is not returned: its methods and properties are not available.116117```js118import {execaSync} from 'execa';119120const {stdout} = execaSync`npm run build`;121```122123Synchronous execution is generally discouraged as it holds the CPU and prevents parallelization. Also, the following features cannot be used:124- Streams: [`subprocess.stdin`](../readme.md#subprocessstdin), [`subprocess.stdout`](../readme.md#subprocessstdout), [`subprocess.stderr`](../readme.md#subprocessstderr), [`subprocess.readable()`](../readme.md#subprocessreadablereadableoptions), [`subprocess.writable()`](../readme.md#subprocesswritablewritableoptions), [`subprocess.duplex()`](../readme.md#subprocessduplexduplexoptions).125- The [`stdin`](../readme.md#optionsstdin), [`stdout`](../readme.md#optionsstdout), [`stderr`](../readme.md#optionsstderr) and [`stdio`](../readme.md#optionsstdio) options cannot be [`'overlapped'`](../readme.md#optionsstdout), an [async iterable](lines.md#progressive-splitting), an async [transform](transform.md), a [`Duplex`](transform.md#duplextransform-streams), nor a [web stream](streams.md#web-streams). Node.js streams can be passed but only if either they [have a file descriptor](streams.md#file-descriptors), or the [`input`](../readme.md#optionsinput) option is used.126- Signal termination: [`subprocess.kill()`](../readme.md#subprocesskillerror), [`subprocess.pid`](../readme.md#subprocesspid), [`cleanup`](../readme.md#optionscleanup) option, [`cancelSignal`](../readme.md#optionscancelsignal) option, [`forceKillAfterDelay`](../readme.md#optionsforcekillafterdelay) option.127- Piping multiple subprocesses: [`subprocess.pipe()`](../readme.md#subprocesspipefile-arguments-options).128- [`subprocess.iterable()`](lines.md#progressive-splitting).129- [`ipc`](../readme.md#optionsipc) and [`serialization`](../readme.md#optionsserialization) options.130- [`result.all`](../readme.md#resultall) is not interleaved.131- [`detached`](../readme.md#optionsdetached) option.132- The [`maxBuffer`](../readme.md#optionsmaxbuffer) option is always measured in bytes, not in characters, [lines](../readme.md#optionslines) nor [objects](transform.md#object-mode). Also, it ignores transforms and the [`encoding`](../readme.md#optionsencoding) option.133134<hr>135136[**Next**: 💬 Escaping/quoting](escaping.md)\137[**Top**: Table of contents](../readme.md#documentation)