Skip to content
Myr

output.md

This file

Path

docs/output.md

198 lines · 8.6 KiB · Markdown

Last changed by

Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360

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–57why, 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```57

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

lines 58–59why, one step back
58## Any output type
59

Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360

line 60why, one step back
60If 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.ipcOutput`](api.md#resultipcoutput) array contains all the messages sent by the subprocess.

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

lines 61–65why, one step back
61
62```js63// main.js64import {execaNode} from 'execa';65

Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360

lines 66–68why, one step back
66const {ipcOutput} = await execaNode`build.js`;
67console.log(ipcOutput[0]); // {kind: 'start', timestamp: date}68console.log(ipcOutput[1]); // {kind: 'stop', timestamp: date}

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

lines 69–79why, one step back
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```79

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 80–81why, one step back
80## Multiple targets
81

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

line 82why, one step back
82The 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 back
83
84The 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```js

Improve documentation (#1003)ehmickybefore adoptionc8a6ed7

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

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 88–94why, one step back
88console.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 output94

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

lines 95–97why, one step back
95If 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 back
98
99`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 options126

Log IPC messages with `verbose: 'full'` (#1063)ehmickybefore adoption839c2fe

line 127why, one step back
127Some 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 back
128
129```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 descriptors138

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

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

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

line 141why, one step back
141[`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 back
142
143```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## Shortcut152

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

line 153why, one step back
153The [`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 back
154
155```js156await execa({stdio: 'ignore'})`npm run build`;157// Same as:158await execa({stdin: 'ignore', stdout: 'ignore', stderr: 'ignore'})`npm run build`;159```160161## Big output162

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

line 163why, one step back
163To 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 back
164

Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360

line 165why, one step back
165When 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.ipcOutput`](api.md#resultipcoutput).

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 166–171why, one step back
166
167This 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.

Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360

line 172why, one step back
172- With [`error.ipcOutput`](ipc.md#retrieve-all-messages): in messages.

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 173–189why, one step back
173
174```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 memory189

Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360

line 190why, one step back
190When 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.ipcOutput`](api.md#resultipcoutput) properties are empty.

Add user guides (#989)ehmickybefore adoptionbe91ecf

line 191why, one step back
191

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

line 192why, one step back
192This 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 back
193
194<hr>195196[**Next**: 📃 Text lines](lines.md)\197[**Previous**: 🎹 Input](input.md)\198[**Top**: Table of contents](../readme.md#documentation)