Skip to content
Myr

output.md

This file

Path

docs/output.md

175 lines · 7.7 KiB · Markdown

Last changed by

Improve documentation of `error.*` fields (#1006)ehmickybefore adoption4ee0dec

History of this file

Take it

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 1–10why, one step back
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>67# 📢 Output89## Stdout and stderr10

Move API reference to its own page (#996)ehmickybefore adoption7cde496

line 11why, one step back
11The [`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 back
12
13```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```js

Improve documentation (#1003)ehmickybefore adoptionc8a6ed7

line 32why, one step back
32await execa({stdout: {file: 'output.txt'}})`npm run build`;

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 33–34why, one step back
33// 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 back
35
36// 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–59why, one step back
39```
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```5758## Multiple targets59

Move API reference to its own page (#996)ehmickybefore adoption7cde496

line 60why, one step back
60The 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 61–64why, one step back
61
62The 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).6364```js

Improve documentation (#1003)ehmickybefore adoptionc8a6ed7

line 65why, one step back
65const {stdout} = await execa({stdout: ['inherit', {file: 'output.txt'}, 'pipe']})`npm run build`;

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 66–72why, one step back
66console.log(stdout);
67```6869When 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.7071## Interleaved output72

Move API reference to its own page (#996)ehmickybefore adoption7cde496

lines 73–75why, one step back
73If 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:
74- [`result.all`](api.md#resultall): [`result.stdout`](api.md#resultstdout) + [`result.stderr`](api.md#resultstderr)75- [`subprocess.all`](api.md#subprocessall): [`subprocess.stdout`](api.md#subprocessstdout) + [`subprocess.stderr`](api.md#subprocessstderr)

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 76–104why, one step back
76
77`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.7879For example, this prints `1 3 2` instead of `1 2 3` because both `console.log()` are merged into a single write.8081```js82const {all} = await execa({all: true})`node example.js`;83```8485```js86// example.js87console.log('1'); // writes to stdout88console.error('2'); // writes to stderr89console.log('3'); // writes to stdout90```9192This can be worked around by using `setTimeout()`.9394```js95import {setTimeout} from 'timers/promises';9697console.log('1');98console.error('2');99await setTimeout(0);100console.log('3');101```102103## Stdout/stderr-specific options104

Move API reference to its own page (#996)ehmickybefore adoption7cde496

line 105why, one step back
105Some 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 106–116why, one step back
106
107```js108// Same value for stdout and stderr109await execa({verbose: 'full'})`npm run build`;110111// Different values for stdout and stderr112await execa({verbose: {stdout: 'none', stderr: 'full'}})`npm run build`;113```114115## Additional file descriptors116

Move API reference to its own page (#996)ehmickybefore adoption7cde496

line 117why, one step back
117The [`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 118why, one step back
118

Move API reference to its own page (#996)ehmickybefore adoption7cde496

line 119why, one step back
119[`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 120–130why, one step back
120
121```js122// Retrieve output from file descriptor number 3123const {stdio} = await execa({124 stdio: ['pipe', 'pipe', 'pipe', 'pipe'],125})`npm run build`;126console.log(stdio[3]);127```128129## Shortcut130

Move API reference to its own page (#996)ehmickybefore adoption7cde496

line 131why, one step back
131The [`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 132–140why, one step back
132
133```js134await execa({stdio: 'ignore'})`npm run build`;135// Same as:136await execa({stdin: 'ignore', stdout: 'ignore', stderr: 'ignore'})`npm run build`;137```138139## Big output140

Move API reference to its own page (#996)ehmickybefore adoption7cde496

line 141why, one step back
141To 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 142why, one step back
142

Improve documentation of `error.*` fields (#1006)ehmickybefore adoption4ee0dec

line 143why, one step back
143When 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) and [`error.stderr`](api.md#resultstderr).

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 144–166why, one step back
144
145This is measured:146- By default: in [characters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length).147- If the [`encoding`](binary.md#encoding) option is `'buffer'`: in bytes.148- If the [`lines`](lines.md#simple-splitting) option is `true`: in lines.149- If a [transform in object mode](transform.md#object-mode) is used: in objects.150151```js152try {153 await execa({maxBuffer: 1_000_000})`npm run build`;154} catch (error) {155 if (error.isMaxBuffer) {156 console.error('Error: output larger than 1MB.');157 console.error(error.stdout);158 console.error(error.stderr);159 }160161 throw error;162}163```164165## Low memory166

Move API reference to its own page (#996)ehmickybefore adoption7cde496

line 167why, one step back
167When 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 168–175why, one step back
168
169This 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.170171<hr>172173[**Next**: 📃 Text lines](lines.md)\174[**Previous**: 🎹 Input](input.md)\175[**Top**: Table of contents](../readme.md#documentation)