input.md
This file
Path
docs/input.md
155 lines · 5.9 KiB · Markdown
Last changed by
Fix documentation and types mistakesSindre Sorhusbefore adoption74b854a
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156<picture> <source media="(prefers-color-scheme: dark)" srcset="../media/logo_dark.svg"> <img alt="execa logo" src="../media/logo.svg" width="400"></picture><br>
# 🎹 Input
## Command arguments
The simplest way to pass input to a subprocess is to use command arguments.
```jsimport {execa} from 'execa';
const commandArgument = 'build';await execa`node child.js ${commandArgument}`;```
If the subprocess is a Node.js file, those are available using [`process.argv`](https://nodejs.org/api/process.html#processargv).
```js// child.jsimport process from 'node:process';
const commandArgument = process.argv[2];```
## Environment variables
Unlike [command arguments](#command-arguments), [environment variables](https://en.wikipedia.org/wiki/Environment_variable) have names. They are commonly used to configure applications.
If the subprocess spawns its own subprocesses, they inherit environment variables. To isolate subprocesses from each other, either command arguments or [`stdin`](#string-input) should be preferred instead.
```js// Keep the current process' environment variables, and set `NO_COLOR`await execa({env: {NO_COLOR: 'true'}})`node child.js`;// Discard the current process' environment variables, only pass `NO_COLOR`await execa({env: {NO_COLOR: 'true'}, extendEnv: false})`node child.js`;```
If the subprocess is a Node.js file, environment variables are available using [`process.env`](https://nodejs.org/api/process.html#processenv).
```js// child.jsimport process from 'node:process';
console.log(process.env.NO_COLOR);```
## String input
Alternatively, input can be provided to [`stdin`](https://en.wikipedia.org/wiki/Standard_streams#Standard_input_(stdin)). Unlike [command arguments](#command-arguments) and [environment variables](#environment-variables) which have [size](https://unix.stackexchange.com/questions/120642/what-defines-the-maximum-size-for-a-command-single-argument) [limits](https://stackoverflow.com/questions/1078031/what-is-the-maximum-size-of-a-linux-environment-variable-value), `stdin` works when the input is big. Also, the input can be redirected from the [terminal](#terminal-input), a [file](#file-input), another [subprocess](pipe.md) or a [stream](streams.md#manual-streaming). Finally, this is required when the input might contain [null bytes](https://en.wikipedia.org/wiki/Null_character), for example when it might be [binary](binary.md#binary-input).
If the input is already available as a string, it can be passed directly to the [`input`](api.md#optionsinput) option.
```jsawait execa({input: 'stdinInput'})`npm run scaffold`;```
The [`stdin`](api.md#optionsstdin) option can also be used, although the string must be wrapped in two arrays for [syntax reasons](output.md#multiple-targets).
```jsawait execa({stdin: [['stdinInput']]})`npm run scaffold`;```
## Ignore input
```jsconst subprocess = execa({stdin: 'ignore'})`npm run scaffold`;console.log(subprocess.stdin); // nullawait subprocess;```
## File input
```jsawait execa({inputFile: 'input.txt'})`npm run scaffold`;// Or:await execa({stdin: {file: 'input.txt'}})`npm run scaffold`;// Or:await execa({stdin: new URL('file:///path/to/input.txt')})`npm run scaffold`;```
## Terminal input
The parent process' input can be re-used in the subprocess by passing `'inherit'`. This is especially useful to receive interactive input in command line applications.
```jsawait execa({stdin: 'inherit'})`npm run scaffold`;```
## Any input type
If the subprocess [uses Node.js](node.md), [almost any type](ipc.md#message-type) can be passed to the subprocess using the [`ipcInput`](ipc.md#send-an-initial-message) option. The subprocess retrieves that input using [`getOneMessage()`](api.md#getonemessagegetonemessageoptions).
```js// main.jsimport {execaNode} from 'execa';
const ipcInput = [ {task: 'lint', ignore: /test\.js/}, {task: 'copy', files: new Set(['main.js', 'index.js']),}];await execaNode({ipcInput})`build.js`;```
```js// build.jsimport {getOneMessage} from 'execa';
const ipcInput = await getOneMessage();```
## Multiple inputs
The input can come from multiple sources by setting the [`stdin`](api.md#optionsstdin) option to an array of values. Alternatively the [`input`](api.md#optionsinput), [`inputFile`](api.md#optionsinputfile) and [`stdin`](api.md#optionsstdin) options can be combined.
The following example redirects `stdin` from both the [terminal](#terminal-input) and an `input.txt` [file](#file-input).
```jsawait execa({stdin: ['inherit', {file: 'input.txt'}]})`npm run scaffold`;```
__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.
## Additional file descriptors
The [`stdio`](api.md#optionsstdio) option can be used to pass some input to any [file descriptor](https://en.wikipedia.org/wiki/File_descriptor), as opposed to only [`stdin`](api.md#optionsstdin).
```js// Pass input to the file descriptor number 3await execa({ stdio: ['pipe', 'pipe', 'pipe', new Uint8Array([/* ... */])],})`npm run build`;```
Some values have an ambiguous direction on an additional file descriptor: Execa cannot tell whether they are meant for input or output, so they default to output. This applies to `'pipe'`, `'inherit'`, [files](output.md#file-output) and [transforms](transform.md). To use one for input instead, such as when [streaming](streams.md#different-file-descriptor) to it, wrap it as `{value, input: true}`.
```jsimport {pipeline} from 'node:stream/promises';
// Stream input to the file descriptor number 3const subprocess = execa({ stdio: ['pipe', 'pipe', 'pipe', {value: 'pipe', input: true}],})`npm run build`;
await pipeline(inputStream, subprocess.writable({to: 'fd3'}));```
<hr>
[**Next**: 📢 Output](output.md)\[**Previous**: 🏁 Termination](termination.md)\[**Top**: Table of contents](../readme.md#documentation)