output.md
This file
Path
docs/output.md
198 lines · 8.6 KiB · Markdown
Last changed by
Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
History of this fileTake it
Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 1–10why, 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# 📢 Output89## Stdout and stderr10Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 11why, one step back11The [`stdout`](api.md#optionsstdout) and [`stderr`](api.md#optionsstderr) options redirect the subprocess output. They default to `'pipe'`, which returns the output using [`result.stdout`](api.md#resultstdout) and [`result.stderr`](api.md#resultstderr).Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 12–31why, one step back1213```js14import {execa} from 'execa';1516const {stdout, stderr} = await execa`npm run build`;17console.log(stdout);18console.log(stderr);19```2021## Ignore output2223```js24const {stdout, stderr} = await execa({stdout: 'ignore'})`npm run build`;25console.log(stdout); // undefined26console.log(stderr); // string with errors27```2829## File output3031```jsImprove documentation (#1003)ehmickybefore adoptionc8a6ed7
line 32why, one step back32await execa({stdout: {file: 'output.txt'}})`npm run build`;Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 33–34why, one step back33// Or:34await execa({stdout: new URL('file:///path/to/output.txt')})`npm run build`;Fix passing the same file or stream to both `stdout` and `stderr` (#1004)ehmickybefore adoptionea889c7
lines 35–38why, one step back3536// Redirect interleaved stdout and stderr to same file37const output = {file: 'output.txt'};38await execa({stdout: output, stderr: output})`npm run build`;Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 39–57why, one step back39```4041## Terminal output4243The parent process' output can be re-used in the subprocess by passing `'inherit'`. This is especially useful to print to the terminal in command line applications.4445```js46await execa({stdout: 'inherit', stderr: 'inherit'})`npm run build`;47```4849To redirect from/to a different [file descriptor](https://en.wikipedia.org/wiki/File_descriptor), pass its [number](https://en.wikipedia.org/wiki/Standard_streams) or [`process.stdout`](https://nodejs.org/api/process.html#processstdout)/[`process.stderr`](https://nodejs.org/api/process.html#processstderr).5051```js52// Print both stdout/stderr to the parent stdout53await execa({stdout: process.stdout, stderr: process.stdout})`npm run build`;54// Or:55await execa({stdout: 1, stderr: 1})`npm run build`;56```57Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
lines 58–79why, one step back58## Any output type5960If the subprocess uses Node.js, [IPC](ipc.md) can be used to return [almost any type](ipc.md#message-type) from the subprocess. The [`result.ipc`](api.md#resultipc) array contains all the messages sent by the subprocess.6162```js63// main.js64import {execaNode} from 'execa';6566const {ipc} = await execaNode`build.js`;67console.log(ipc[0]); // {kind: 'start', timestamp: date}68console.log(ipc[1]); // {kind: 'stop', timestamp: date}69```7071```js72// build.js73import {sendMessage} from 'execa';7475await sendMessage({kind: 'start', timestamp: new Date()});76await runBuild();77await sendMessage({kind: 'stop', timestamp: new Date()});78```79Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 80–81why, one step back80## Multiple targets81Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 82why, one step back82The output can be redirected to multiple targets by setting the [`stdout`](api.md#optionsstdout) or [`stderr`](api.md#optionsstderr) option to an array of values. This also allows specifying multiple inputs with the [`stdin`](api.md#optionsstdin) option.Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 83–86why, one step back8384The following example redirects `stdout` to both the [terminal](#terminal-output) and an `output.txt` [file](#file-output), while also retrieving its value [programmatically](#stdout-and-stderr).8586```jsImprove documentation (#1003)ehmickybefore adoptionc8a6ed7
line 87why, one step back87const {stdout} = await execa({stdout: ['inherit', {file: 'output.txt'}, 'pipe']})`npm run build`;Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 88–94why, one step back88console.log(stdout);89```9091When combining [`'inherit'`](#terminal-output) with other values, please note that the subprocess will not be an interactive TTY, even if the current process is one.9293## Interleaved output94Move API reference to its own page (#996)ehmickybefore adoption7cde496
lines 95–97why, one step back95If the [`all`](api.md#optionsall) option is `true`, [`stdout`](https://en.wikipedia.org/wiki/Standard_streams#Standard_output_(stdout)) and [`stderr`](https://en.wikipedia.org/wiki/Standard_streams#Standard_error_(stderr)) are combined:96- [`result.all`](api.md#resultall): [`result.stdout`](api.md#resultstdout) + [`result.stderr`](api.md#resultstderr)97- [`subprocess.all`](api.md#subprocessall): [`subprocess.stdout`](api.md#subprocessstdout) + [`subprocess.stderr`](api.md#subprocessstderr)Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 98–126why, one step back9899`stdout` and `stderr` are guaranteed to interleave. However, for performance reasons, the subprocess might buffer and merge multiple simultaneous writes to `stdout` or `stderr`. This can prevent proper interleaving.100101For example, this prints `1 3 2` instead of `1 2 3` because both `console.log()` are merged into a single write.102103```js104const {all} = await execa({all: true})`node example.js`;105```106107```js108// example.js109console.log('1'); // writes to stdout110console.error('2'); // writes to stderr111console.log('3'); // writes to stdout112```113114This can be worked around by using `setTimeout()`.115116```js117import {setTimeout} from 'timers/promises';118119console.log('1');120console.error('2');121await setTimeout(0);122console.log('3');123```124125## Stdout/stderr-specific options126Log IPC messages with `verbose: 'full'` (#1063)ehmickybefore adoption839c2fe
line 127why, one step back127Some options are related to the subprocess output: [`verbose`](api.md#optionsverbose), [`lines`](api.md#optionslines), [`stripFinalNewline`](api.md#optionsstripfinalnewline), [`buffer`](api.md#optionsbuffer), [`maxBuffer`](api.md#optionsmaxbuffer). By default, those options apply to all [file descriptors](https://en.wikipedia.org/wiki/File_descriptor) ([`stdout`](https://en.wikipedia.org/wiki/Standard_streams#Standard_output_(stdout)), [`stderr`](https://en.wikipedia.org/wiki/Standard_streams#Standard_error_(stderr)), and [others](#additional-file-descriptors)) and [IPC messages](ipc.md). A plain object can be passed instead to apply them to only `stdout`, `stderr`, `all` (both stdout and stderr), [`ipc`](ipc.md), [`fd3`](#additional-file-descriptors), etc.Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 128–138why, one step back128129```js130// Same value for stdout and stderr131await execa({verbose: 'full'})`npm run build`;132133// Different values for stdout and stderr134await execa({verbose: {stdout: 'none', stderr: 'full'}})`npm run build`;135```136137## Additional file descriptors138Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 139why, one step back139The [`stdio`](api.md#optionsstdio) option is an array combining [`stdin`](api.md#optionsstdin), [`stdout`](api.md#optionsstdout), [`stderr`](api.md#optionsstderr) and any other file descriptor. It is useful when using additional [file descriptors](https://en.wikipedia.org/wiki/File_descriptor) beyond the [standard ones](https://en.wikipedia.org/wiki/Standard_streams), either for [input](input.md#additional-file-descriptors) or output.Add user guides (#989)ehmickybefore adoptionbe91ecf
line 140why, one step back140Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 141why, one step back141[`result.stdio`](api.md#resultstdio) can be used to retrieve some output from any file descriptor, as opposed to only [`stdout`](api.md#optionsstdout) and [`stderr`](api.md#optionsstderr).Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 142–152why, one step back142143```js144// Retrieve output from file descriptor number 3145const {stdio} = await execa({146 stdio: ['pipe', 'pipe', 'pipe', 'pipe'],147})`npm run build`;148console.log(stdio[3]);149```150151## Shortcut152Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 153why, one step back153The [`stdio`](api.md#optionsstdio) option can also be a single value [`'pipe'`](#stdout-and-stderr), [`'overlapped'`](windows.md#asynchronous-io), [`'ignore'`](#ignore-output) or [`'inherit'`](#terminal-output). This is a shortcut for setting that same value with the [`stdin`](api.md#optionsstdin), [`stdout`](api.md#optionsstdout) and [`stderr`](api.md#optionsstderr) options.Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 154–162why, one step back154155```js156await execa({stdio: 'ignore'})`npm run build`;157// Same as:158await execa({stdin: 'ignore', stdout: 'ignore', stderr: 'ignore'})`npm run build`;159```160161## Big output162Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 163why, one step back163To prevent high memory consumption, a maximum output size can be set using the [`maxBuffer`](api.md#optionsmaxbuffer) option. It defaults to 100MB.Add user guides (#989)ehmickybefore adoptionbe91ecf
line 164why, one step back164Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 165why, one step back165When this threshold is hit, the subprocess fails and [`error.isMaxBuffer`](api.md#errorismaxbuffer) becomes `true`. The truncated output is still available using [`error.stdout`](api.md#resultstdout), [`error.stderr`](api.md#resultstderr) and [`error.ipc`](api.md#resultipc).Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 166–171why, one step back166167This is measured:168- By default: in [characters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length).169- If the [`encoding`](binary.md#encoding) option is `'buffer'`: in bytes.170- If the [`lines`](lines.md#simple-splitting) option is `true`: in lines.171- If a [transform in object mode](transform.md#object-mode) is used: in objects.Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 172why, one step back172- With [`error.ipc`](ipc.md#retrieve-all-messages): in messages.Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 173–189why, one step back173174```js175try {176 await execa({maxBuffer: 1_000_000})`npm run build`;177} catch (error) {178 if (error.isMaxBuffer) {179 console.error('Error: output larger than 1MB.');180 console.error(error.stdout);181 console.error(error.stderr);182 }183184 throw error;185}186```187188## Low memory189Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 190why, one step back190When the [`buffer`](api.md#optionsbuffer) option is `false`, [`result.stdout`](api.md#resultstdout), [`result.stderr`](api.md#resultstderr), [`result.all`](api.md#resultall), [`result.stdio[*]`](api.md#resultstdio) and [`result.ipc`](api.md#resultipc) properties are empty.Add user guides (#989)ehmickybefore adoptionbe91ecf
line 191why, one step back191Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 192why, one step back192This prevents high memory consumption when the output is big. However, the output must be either ignored, [redirected](#file-output), [streamed](streams.md) or [listened to](ipc.md#listening-to-messages). If streamed, this should be done right away to avoid missing any data.Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 193–198why, one step back193194<hr>195196[**Next**: 📃 Text lines](lines.md)\197[**Previous**: 🎹 Input](input.md)\198[**Top**: Table of contents](../readme.md#documentation)