Skip to content
Myr

output.md

This file

Path

docs/output.md

205 lines · 8.7 KiB · Markdown

Last changed by

[docs] A process is not a TTY usually, just connected to one. (#1208)M.K.before adoption3e8fa32

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–64why, one step back
46```
4748## Terminal output4950The 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.5152```js53await execa({stdout: 'inherit', stderr: 'inherit'})`npm run build`;54```5556To 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).5758```js59// Print both stdout/stderr to the parent stdout60await execa({stdout: process.stdout, stderr: process.stdout})`npm run build`;61// Or:62await execa({stdout: 1, stderr: 1})`npm run build`;63```64

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

lines 65–66why, one step back
65## Any output type
66

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

line 67why, one step back
67If 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 68–72why, one step back
68
69```js70// main.js71import {execaNode} from 'execa';72

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

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

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

lines 76–86why, one step back
76```
7778```js79// build.js80import {sendMessage} from 'execa';8182await sendMessage({kind: 'start', timestamp: new Date()});83await runBuild();84await sendMessage({kind: 'stop', timestamp: new Date()});85```86

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 87–88why, one step back
87## Multiple targets
88

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

line 89why, one step back
89The 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 90–93why, one step back
90
91The 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).9293```js

Improve documentation (#1003)ehmickybefore adoptionc8a6ed7

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

Add user guides (#989)ehmickybefore adoptionbe91ecf

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

[docs] A process is not a TTY usually, just connected to one. (#1208)M.K.before adoption3e8fa32

line 98why, one step back
98When combining [`'inherit'`](#terminal-output) with other values, please note that the subprocess will not be connected to an interactive TTY, even if the current process is.

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 99–101why, one step back
99
100## Interleaved output101

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

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

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 105–133why, one step back
105
106`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.107108For example, this prints `1 3 2` instead of `1 2 3` because both `console.log()` are merged into a single write.109110```js111const {all} = await execa({all: true})`node example.js`;112```113114```js115// example.js116console.log('1'); // writes to stdout117console.error('2'); // writes to stderr118console.log('3'); // writes to stdout119```120121This can be worked around by using `setTimeout()`.122123```js124import {setTimeout} from 'timers/promises';125126console.log('1');127console.error('2');128await setTimeout(0);129console.log('3');130```131132## Stdout/stderr-specific options133

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

line 134why, one step back
134Some 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 135–145why, one step back
135
136```js137// Same value for stdout and stderr138await execa({verbose: 'full'})`npm run build`;139140// Different values for stdout and stderr141await execa({verbose: {stdout: 'none', stderr: 'full'}})`npm run build`;142```143144## Additional file descriptors145

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

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

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

line 148why, one step back
148[`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 149–159why, one step back
149
150```js151// Retrieve output from file descriptor number 3152const {stdio} = await execa({153 stdio: ['pipe', 'pipe', 'pipe', 'pipe'],154})`npm run build`;155console.log(stdio[3]);156```157158## Shortcut159

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

line 160why, one step back
160The [`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 161–169why, one step back
161
162```js163await execa({stdio: 'ignore'})`npm run build`;164// Same as:165await execa({stdin: 'ignore', stdout: 'ignore', stderr: 'ignore'})`npm run build`;166```167168## Big output169

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

line 170why, one step back
170To 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 171why, one step back
171

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

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

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

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

Add user guides (#989)ehmickybefore adoptionbe91ecf

lines 180–196why, one step back
180
181```js182try {183 await execa({maxBuffer: 1_000_000})`npm run build`;184} catch (error) {185 if (error.isMaxBuffer) {186 console.error('Error: output larger than 1MB.');187 console.error(error.stdout);188 console.error(error.stderr);189 }190191 throw error;192}193```194195## Low memory196

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

line 197why, one step back
197When 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 198why, one step back
198

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

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