Skip to content
Myr

output.md

This file

Path

docs/output.md

207 lines · 8.8 KiB · Markdown

Last changed by

Fix duplicated or conflicting file descriptor targetsSindre Sorhusbefore adoption587045a

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`;

Allow appending to files (#1166)ehmickybefore adoption94a75df

line 35why, one step back
35```

Fix passing the same file or stream to both `stdout` and `stderr` (#1004)ehmickybefore adoptionea889c7

line 36why, one step back
36

Allow appending to files (#1166)ehmickybefore adoption94a75df

line 37why, one step back
37```js

Fix passing the same file or stream to both `stdout` and `stderr` (#1004)ehmickybefore adoptionea889c7

lines 38–40why, one step back
38// Redirect interleaved stdout and stderr to same file
39const output = {file: 'output.txt'};40await execa({stdout: output, stderr: output})`npm run build`;

Allow appending to files (#1166)ehmickybefore adoption94a75df

lines 41–45why, one step back
41```
4243```js44// Append instead of overwriting45await execa({stdout: {file: 'output.txt', append: true}})`npm run build`;

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 46–47why, one step back
46```
47

Fix duplicated or conflicting file descriptor targetsSindre Sorhusbefore adoption587045a

lines 48–49why, one step back
48When several file descriptors target the same file, the first one decides whether it is appended to.
49

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 50–66why, one step back
50## Terminal output
5152The 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.5354```js55await execa({stdout: 'inherit', stderr: 'inherit'})`npm run build`;56```5758To 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).5960```js61// Print both stdout/stderr to the parent stdout62await execa({stdout: process.stdout, stderr: process.stdout})`npm run build`;63// Or:64await execa({stdout: 1, stderr: 1})`npm run build`;65```66

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

lines 67–68why, one step back
67## Any output type
68

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

line 69why, one step back
69If 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 70–74why, one step back
70
71```js72// main.js73import {execaNode} from 'execa';74

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

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

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

lines 78–88why, one step back
78```
7980```js81// build.js82import {sendMessage} from 'execa';8384await sendMessage({kind: 'start', timestamp: new Date()});85await runBuild();86await sendMessage({kind: 'stop', timestamp: new Date()});87```88

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 89–90why, one step back
89## Multiple targets
90

Document support for multiple inputs (#1222)ehmickybefore adoption6ad1a6e

line 91why, one step back
91The 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.

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 92–95why, one step back
92
93The 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).9495```js

Improve documentation (#1003)ehmickybefore adoptionc8a6ed7

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

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 97–99why, one step back
97console.log(stdout);
98```99

[docs] TTY control is lost only on the FD that uses mixed inherit. (#1209)M.K.before adoptione7cafeb

line 100why, one step back
100__Loss of TTY control:__ Please note that when a file descriptor is configured with a combination of 'inherit' and other values, this file descriptor will never refer to a TTY in the subprocess, even if in the current process it does.

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 101–103why, one step back
101
102## Interleaved output103

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

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

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 107–135why, one step back
107
108`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.109110For example, this prints `1 3 2` instead of `1 2 3` because both `console.log()` are merged into a single write.111112```js113const {all} = await execa({all: true})`node example.js`;114```115116```js117// example.js118console.log('1'); // writes to stdout119console.error('2'); // writes to stderr120console.log('3'); // writes to stdout121```122123This can be worked around by using `setTimeout()`.124125```js126import {setTimeout} from 'timers/promises';127128console.log('1');129console.error('2');130await setTimeout(0);131console.log('3');132```133134## Stdout/stderr-specific options135

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

line 136why, one step back
136Some 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 137–147why, one step back
137
138```js139// Same value for stdout and stderr140await execa({verbose: 'full'})`npm run build`;141142// Different values for stdout and stderr143await execa({verbose: {stdout: 'none', stderr: 'full'}})`npm run build`;144```145146## Additional file descriptors147

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

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

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

line 150why, one step back
150[`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 151–161why, one step back
151
152```js153// Retrieve output from file descriptor number 3154const {stdio} = await execa({155 stdio: ['pipe', 'pipe', 'pipe', 'pipe'],156})`npm run build`;157console.log(stdio[3]);158```159160## Shortcut161

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

line 162why, one step back
162The [`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 163–171why, one step back
163
164```js165await execa({stdio: 'ignore'})`npm run build`;166// Same as:167await execa({stdin: 'ignore', stdout: 'ignore', stderr: 'ignore'})`npm run build`;168```169170## Big output171

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

line 172why, one step back
172To 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 173why, one step back
173

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

line 174why, one step back
174When 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 175–180why, one step back
175
176This is measured:177- By default: in [characters](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/length).178- If the [`encoding`](binary.md#encoding) option is `'buffer'`: in bytes.179- If the [`lines`](lines.md#simple-splitting) option is `true`: in lines.180- If a [transform in object mode](transform.md#object-mode) is used: in objects.

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

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

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 182–198why, one step back
182
183```js184try {185 await execa({maxBuffer: 1_000_000})`npm run build`;186} catch (error) {187 if (error.isMaxBuffer) {188 console.error('Error: output larger than 1MB.');189 console.error(error.stdout);190 console.error(error.stderr);191 }192193 throw error;194}195```196197## Low memory198

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

line 199why, one step back
199When 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 200why, one step back
200

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

line 201why, one step back
201This 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 202–207why, one step back
202
203<hr>204205[**Next**: 📃 Text lines](lines.md)\206[**Previous**: 🎹 Input](input.md)\207[**Top**: Table of contents](../readme.md#documentation)