output.md
This file
Path
docs/output.md
208 lines · 9.0 KiB · Markdown
Last changed by
Fix documentation and types mistakesSindre Sorhusbefore adoption74b854a
History of this fileTake it
The walk stopped after 200 commits: lines older than 9f65b86 point at it.
100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 1–34why, 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 stderr1011The [`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).1213```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```js32await execa({stdout: {file: 'output.txt'}})`npm run build`;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 back35```100% test coverage (#1018)ehmickybefore adoption9f65b86
line 36why, one step back36Allow appending to files (#1166)ehmickybefore adoption94a75df
line 37why, one step back37```js100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 38–40why, one step back38// Redirect interleaved stdout and stderr to same file39const 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 back41```4243```js44// Append instead of overwriting45await execa({stdout: {file: 'output.txt', append: true}})`npm run build`;100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 46–47why, one step back46```47Fix duplicated or conflicting file descriptor targetsSindre Sorhusbefore adoption587045a
lines 48–49why, one step back48When several file descriptors target the same file, the first one decides whether it is appended to.49100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 50–66why, one step back50## Terminal output5152The 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```66Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
lines 67–68why, one step back67## Any output type68Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360
line 69why, one step back69If 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 back7071```js72// main.js73import {execaNode} from 'execa';74Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360
lines 75–77why, one step back75const {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 back78```7980```js81// build.js82import {sendMessage} from 'execa';8384await sendMessage({kind: 'start', timestamp: new Date()});85await runBuild();86await sendMessage({kind: 'stop', timestamp: new Date()});87```88100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 89–90why, one step back89## Multiple targets90Document support for multiple inputs (#1222)ehmickybefore adoption6ad1a6e
line 91why, one step back91The 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.100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 92–99why, one step back9293The 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```js96const {stdout} = await execa({stdout: ['inherit', {file: 'output.txt'}, 'pipe']})`npm run build`;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 back100__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.100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 101–135why, one step back101102## Interleaved output103104If 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)107108`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 options135Log IPC messages with `verbose: 'full'` (#1063)ehmickybefore adoption839c2fe
line 136why, one step back136Some 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.100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 137–173why, one step back137138```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 descriptors147148The [`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.149150[`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).151152```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## Shortcut161162The [`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.163164```js165await execa({stdio: 'ignore'})`npm run build`;166// Same as:167await execa({stdin: 'ignore', stdout: 'ignore', stderr: 'ignore'})`npm run build`;168```169170## Big output171172To prevent high memory consumption, a maximum output size can be set using the [`maxBuffer`](api.md#optionsmaxbuffer) option. It defaults to 100MB.173Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360
line 174why, one step back174When 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).100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 175–180why, one step back175176This 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 back181- With [`error.ipcOutput`](ipc.md#retrieve-all-messages): in messages.Fix documentation and types mistakesSindre Sorhusbefore adoption74b854a
line 182why, one step back182- With [synchronous methods](execution.md#synchronous-execution): in bytes, since `spawnSync()`'s limit is enforced by Node.js itself before the output is decoded or split into lines.100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 183–199why, one step back183184```js185try {186 await execa({maxBuffer: 1_000_000})`npm run build`;187} catch (error) {188 if (error.isMaxBuffer) {189 console.error('Error: output larger than 1MB.');190 console.error(error.stdout);191 console.error(error.stderr);192 }193194 throw error;195}196```197198## Low memory199Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360
line 200why, one step back200When 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.100% test coverage (#1018)ehmickybefore adoption9f65b86
line 201why, one step back201Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 202why, one step back202This 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.100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 203–208why, one step back203204<hr>205206[**Next**: 📃 Text lines](lines.md)\207[**Previous**: 🎹 Input](input.md)\208[**Top**: Table of contents](../readme.md#documentation)