execution.md
This file
Path
docs/execution.md
169 lines · 5.7 KiB · Markdown
Last changed by
TweaksSindre Sorhusbefore adoption698a1b8
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170<picture> <source media="(prefers-color-scheme: dark)" srcset="../media/logo_dark.svg"> <img alt="execa logo" src="../media/logo.svg" width="400"></picture><br>
# ▶️ Basic execution
## Array syntax
```jsimport {execa} from 'execa';
await execa('npm', ['run', 'build']);```
## Template string syntax
All [available methods](api.md#methods) can use either the [array syntax](#array-syntax) or the template string syntax, which are equivalent.
```jsawait execa`npm run build`;```
### String argument
```jsawait execa`npm run ${'task with space'}`;```
### Number argument
```jsawait execa`npm run build --concurrency ${2}`;```
### Subcommands
```jsconst result = await execa`get-concurrency`;
// Uses `result.stdout`await execa`npm run build --concurrency ${result}`;```
### Concatenation
```jsconst tmpDirectory = '/tmp';await execa`mkdir ${tmpDirectory}/filename`;```
### Multiple arguments
```jsconst result = await execa`get-concurrency`;
await execa`npm ${['run', 'build', '--concurrency', result]}`;```
### No arguments
```jsawait execa`npm run build ${[]}`;// Same as:await execa('npm', ['run', 'build']);```
### Empty string argument
```jsawait execa`npm run build ${''}`;// Same as:await execa('npm', ['run', 'build', '']);```
### Conditional argument
```jsconst commandArguments = failFast ? ['--fail-fast'] : [];await execa`npm run build ${commandArguments}`;```
### Multiple lines
```jsawait execa`npm run build --concurrency 2 --fail-fast`;```
### Shells
By default, any shell-specific syntax has no special meaning and does not need to be escaped. This prevents [shell injections](https://en.wikipedia.org/wiki/Code_injection#Shell_injection). [More info.](escaping.md#shells)
```js// This prints `$TASK_NAME`, not `build`await execa({env: {TASK_NAME: 'build'}})`echo $TASK_NAME`;```
## Options
[Options](api.md#options-1) can be passed to influence the execution's behavior.
### Array syntax
```jsawait execa('npm', ['run', 'build'], {timeout: 5000});```
### Template string syntax
```jsawait execa({timeout: 5000})`npm run build`;```
### Global/shared options
```jsconst timedExeca = execa({timeout: 5000});
await timedExeca('npm', ['run', 'build']);await timedExeca`npm run test`;```
## Return value
### Subprocess
The subprocess is returned as soon as it is spawned. It is a `Promise` with [Execa-specific methods and properties](api.md#subprocess). The underlying Node.js [`ChildProcess`](https://nodejs.org/api/child_process.html#class-childprocess) is available as [`subprocess.nodeChildProcess`](api.md#subprocessnodechildprocess).
```jsconst subprocess = execa`npm run build`;console.log(subprocess.pid);```
### Result
The subprocess resolves with the [`result`](api.md#result).
```jsconst {stdout} = await execa`npm run build`;```
### Synchronous execution
[`execaSync()`](api.md#execasyncfile-arguments-options) and [`$.sync()`](api.md#syncfile-arguments-options) return the [`result`](api.md#result) without needing to `await`. The [`subprocess`](#subprocess) is not returned: its methods and properties are not available.
```jsimport {execaSync} from 'execa';
const {stdout} = execaSync`npm run build`;```
Synchronous execution is generally discouraged as it holds the CPU and prevents parallelization. Also, the following features cannot be used:- Streams: [`subprocess.stdin`](api.md#subprocessstdin), [`subprocess.stdout`](api.md#subprocessstdout), [`subprocess.stderr`](api.md#subprocessstderr), [`subprocess.readable()`](api.md#subprocessreadablereadableoptions), [`subprocess.writable()`](api.md#subprocesswritablewritableoptions), [`subprocess.duplex()`](api.md#subprocessduplexduplexoptions).- The [`stdin`](api.md#optionsstdin), [`stdout`](api.md#optionsstdout), [`stderr`](api.md#optionsstderr) and [`stdio`](api.md#optionsstdio) options cannot be [`'overlapped'`](api.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`](api.md#optionsinput) option is used.- Signal termination: [`subprocess.kill()`](api.md#subprocesskillerror), [`subprocess.pid`](api.md#subprocesspid), [`cleanup`](api.md#optionscleanup) option, [`cancelSignal`](api.md#optionscancelsignal) option, [`forceKillAfterDelay`](api.md#optionsforcekillafterdelay) option.- Piping multiple subprocesses: [`subprocess.pipe()`](api.md#subprocesspipefile-arguments-options).- [`subprocess.iterable()`](lines.md#progressive-splitting).- [IPC](ipc.md): [`sendMessage()`](api.md#sendmessagemessage-sendmessageoptions), [`getOneMessage()`](api.md#getonemessagegetonemessageoptions), [`getEachMessage()`](api.md#geteachmessagegeteachmessageoptions), [`result.ipcOutput`](output.md#any-output-type), [`ipc`](api.md#optionsipc) option, [`serialization`](api.md#optionsserialization) option, [`ipcInput`](input.md#any-input-type) option.- [`result.all`](api.md#resultall) is not interleaved. Also, with the [`lines`](api.md#optionslines) option, a file descriptor that uses it keeps its own [`stripFinalNewline`](api.md#optionsstripfinalnewline) value in `result.all`.- [`detached`](api.md#optionsdetached) option.- The [`maxBuffer`](api.md#optionsmaxbuffer) option is always measured in bytes, not in characters, [lines](api.md#optionslines) nor [objects](transform.md#object-mode). Also, it ignores transforms and the [`encoding`](api.md#optionsencoding) option, and the `maxBuffer.stdout` value limits the total output of all file descriptors.
<hr>
[**Next**: 💬 Escaping/quoting](escaping.md)\[**Top**: Table of contents](../readme.md#documentation)