output.md
This file
Path
docs/output.md
171 lines · 7.5 KiB · Markdown
Last changed by
Improve documentation (#1003)ehmickybefore adoptionc8a6ed7
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–55why, one step back33// Or:34await execa({stdout: new URL('file:///path/to/output.txt')})`npm run build`;35```3637## Terminal output3839The 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.4041```js42await execa({stdout: 'inherit', stderr: 'inherit'})`npm run build`;43```4445To 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).4647```js48// Print both stdout/stderr to the parent stdout49await execa({stdout: process.stdout, stderr: process.stdout})`npm run build`;50// Or:51await execa({stdout: 1, stderr: 1})`npm run build`;52```5354## Multiple targets55Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 56why, one step back56The 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 57–60why, one step back5758The 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).5960```jsImprove documentation (#1003)ehmickybefore adoptionc8a6ed7
line 61why, one step back61const {stdout} = await execa({stdout: ['inherit', {file: 'output.txt'}, 'pipe']})`npm run build`;Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 62–68why, one step back62console.log(stdout);63```6465When 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.6667## Interleaved output68Move API reference to its own page (#996)ehmickybefore adoption7cde496
lines 69–71why, one step back69If 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:70- [`result.all`](api.md#resultall): [`result.stdout`](api.md#resultstdout) + [`result.stderr`](api.md#resultstderr)71- [`subprocess.all`](api.md#subprocessall): [`subprocess.stdout`](api.md#subprocessstdout) + [`subprocess.stderr`](api.md#subprocessstderr)Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 72–100why, one step back7273`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.7475For example, this prints `1 3 2` instead of `1 2 3` because both `console.log()` are merged into a single write.7677```js78const {all} = await execa({all: true})`node example.js`;79```8081```js82// example.js83console.log('1'); // writes to stdout84console.error('2'); // writes to stderr85console.log('3'); // writes to stdout86```8788This can be worked around by using `setTimeout()`.8990```js91import {setTimeout} from 'timers/promises';9293console.log('1');94console.error('2');95await setTimeout(0);96console.log('3');97```9899## Stdout/stderr-specific options100Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 101why, one step back101Some 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)). A plain object can be passed instead to apply them to only `stdout`, `stderr`, [`fd3`](#additional-file-descriptors), etc.Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 102–112why, one step back102103```js104// Same value for stdout and stderr105await execa({verbose: 'full'})`npm run build`;106107// Different values for stdout and stderr108await execa({verbose: {stdout: 'none', stderr: 'full'}})`npm run build`;109```110111## Additional file descriptors112Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 113why, one step back113The [`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 114why, one step back114Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 115why, one step back115[`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 116–126why, one step back116117```js118// Retrieve output from file descriptor number 3119const {stdio} = await execa({120 stdio: ['pipe', 'pipe', 'pipe', 'pipe'],121})`npm run build`;122console.log(stdio[3]);123```124125## Shortcut126Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 127why, one step back127The [`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 128–136why, one step back128129```js130await execa({stdio: 'ignore'})`npm run build`;131// Same as:132await execa({stdin: 'ignore', stdout: 'ignore', stderr: 'ignore'})`npm run build`;133```134135## Big output136Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 137why, one step back137To 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 138why, one step back138Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 139why, one step back139When this threshold is hit, the subprocess fails and [`error.isMaxBuffer`](api.md#resultismaxbuffer) becomes `true`. The truncated output is still available using [`error.stdout`](api.md#resultstdout) and [`error.stderr`](api.md#resultstderr).Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 140–162why, one step back140141This is measured:142- By default: in [characters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length).143- If the [`encoding`](binary.md#encoding) option is `'buffer'`: in bytes.144- If the [`lines`](lines.md#simple-splitting) option is `true`: in lines.145- If a [transform in object mode](transform.md#object-mode) is used: in objects.146147```js148try {149 await execa({maxBuffer: 1_000_000})`npm run build`;150} catch (error) {151 if (error.isMaxBuffer) {152 console.error('Error: output larger than 1MB.');153 console.error(error.stdout);154 console.error(error.stderr);155 }156157 throw error;158}159```160161## Low memory162Move API reference to its own page (#996)ehmickybefore adoption7cde496
line 163why, one step back163When the [`buffer`](api.md#optionsbuffer) option is `false`, [`result.stdout`](api.md#resultstdout), [`result.stderr`](api.md#resultstderr), [`result.all`](api.md#resultall) and [`result.stdio[*]`](api.md#resultstdio) properties are not set.Add user guides (#989)ehmickybefore adoptionbe91ecf
lines 164–171why, one step back164165This prevents high memory consumption when the output is big. However, the output must be either ignored, [redirected](#file-output) or [streamed](streams.md). If streamed, this should be done right away to avoid missing any data.166167<hr>168169[**Next**: 📃 Text lines](lines.md)\170[**Previous**: 🎹 Input](input.md)\171[**Top**: Table of contents](../readme.md#documentation)