用于配置子进程与父进程之间建立的管道。 默认情况下,子进程的 stdin、 stdout 和 stderr 会重定向到 ChildProcess 对象上相应的 subprocess.stdinsubprocess.stdoutsubprocess.stderr 流。 等同于将 options.stdio 设为 ['pipe', 'pipe', 'pipe']

options.stdio 可以是以下字符串之一:

  • 'pipe' - 等同于 ['pipe', 'pipe', 'pipe'](默认)。
  • 'ignore' - 等同于 ['ignore', 'ignore', 'ignore']
  • 'inherit' - 等同于 ['inherit', 'inherit', 'inherit'][0,1,2]

option.stdio 的值是一个数组,其中每个索引对应一个子进程 fd。 fd 的 0、1 和 2 分别对应 stdin、stdout 和 stderr。 额外的 fd 可以指定创建父进程和子进程之间的额外管道。 该值是以下之一:

  1. 'pipe' - 创建一个子进程和父进程之间的管道。 管道的父端作为 child_process 对象的属性。 为 fd 创建的管道 0 - 2 分别作为 subprocess.stdinsubprocess.stdoutsubprocess.stderr
  2. 'ipc' - 创建一个用于父进程和子进程之间传递消息或文件描述符的 IPC 通道符。 一个 ChildProcess 最多只能有一个 IPC stdio 文件描述符。 设置该选项可启用 subprocess.send()。 如果子进程是一个 Node.js 进程,则一个已存在的 IPC 通道会在子进程中启用 process.send()process.disconnect(),同时也会启用 'disconnect''message' 事件。
  3. 'ignore' - 指示 Node.js 忽略子进程中的 fd。 由于 Node.js 总是会为它衍生的进程打开 fd 0 - 2,所以设置 fd 为 'ignore' 可以使 Node.js 打开 /dev/null 并将它附加到子进程的 fd 上。
  4. 'inherit' - 从父进程传入或传入父进程相应的 stdio 流。 前三个位置分别等同于 process.stdinprocess.stdoutprocess.stderr。 其他位置等同于 'ignore'
  5. <Stream> 对象 - 共享一个指向子进程的 tty、文件、socket 或管道的可读或可写流。 流的底层文件描述符在子进程中对应 stdio 数组索引的 fd。 该流必须有一个底层描述符(文件流直到 'open' 事件发生才需要)。
  6. 正整数 - 解析成在父进程打开的文件描述符。 与子进程共享,类似于 Stream 对象的共享。
  7. nullundefined - 使用默认值。 对于 stdio 的 fd 0、1 和 2(换言之,stdin、stdout 和 stderr),创建管道。 对于 fd 3 及以上,默认为 'ignore'
const { spawn } = require('child_process');

// 子进程使用父进程的 stdio。
spawn('prg', [], { stdio: 'inherit' });

// 衍生的子进程只共享 stderr。
spawn('prg', [], { stdio: ['pipe', 'pipe', process.stderr] });

// 打开一个额外的 fd=4,用于与程序交互。
spawn('prg', [], { stdio: ['pipe', null, null, null, 'pipe'] });

The options.stdio option is used to configure the pipes that are established between the parent and child process. By default, the child's stdin, stdout, and stderr are redirected to corresponding subprocess.stdin, subprocess.stdout, and subprocess.stderr streams on the ChildProcess object. This is equivalent to setting the options.stdio equal to ['pipe', 'pipe', 'pipe'].

For convenience, options.stdio may be one of the following strings:

  • 'pipe' - equivalent to ['pipe', 'pipe', 'pipe'] (the default)
  • 'ignore' - equivalent to ['ignore', 'ignore', 'ignore']
  • 'inherit' - equivalent to ['inherit', 'inherit', 'inherit'] or [0, 1, 2]

Otherwise, the value of options.stdio is an array where each index corresponds to an fd in the child. The fds 0, 1, and 2 correspond to stdin, stdout, and stderr, respectively. Additional fds can be specified to create additional pipes between the parent and child. The value is one of the following:

  1. 'pipe' - Create a pipe between the child process and the parent process. The parent end of the pipe is exposed to the parent as a property on the child_process object as subprocess.stdio[fd]. Pipes created for fds 0 - 2 are also available as subprocess.stdin, subprocess.stdout and subprocess.stderr, respectively.

  2. 'ipc' - Create an IPC channel for passing messages/file descriptors between parent and child. A ChildProcess may have at most one IPC stdio file descriptor. Setting this option enables the subprocess.send() method. If the child is a Node.js process, the presence of an IPC channel will enable process.send() and process.disconnect() methods, as well as 'disconnect' and 'message' events within the child.

    Accessing the IPC channel fd in any way other than process.send() or using the IPC channel with a child process that is not a Node.js instance is not supported.

  3. 'ignore' - Instructs Node.js to ignore the fd in the child. While Node.js will always open fds 0 - 2 for the processes it spawns, setting the fd to 'ignore' will cause Node.js to open /dev/null and attach it to the child's fd.

  4. 'inherit' - Pass through the corresponding stdio stream to/from the parent process. In the first three positions, this is equivalent to process.stdin, process.stdout, and process.stderr, respectively. In any other position, equivalent to 'ignore'.

  5. <Stream> object - Share a readable or writable stream that refers to a tty, file, socket, or a pipe with the child process. The stream's underlying file descriptor is duplicated in the child process to the fd that corresponds to the index in the stdio array. Note that the stream must have an underlying descriptor (file streams do not until the 'open' event has occurred).

  6. Positive integer - The integer value is interpreted as a file descriptor that is currently open in the parent process. It is shared with the child process, similar to how <Stream> objects can be shared.

  7. null, undefined - Use default value. For stdio fds 0, 1, and 2 (in other words, stdin, stdout, and stderr) a pipe is created. For fd 3 and up, the default is 'ignore'.

const { spawn } = require('child_process');

// Child will use parent's stdios
spawn('prg', [], { stdio: 'inherit' });

// Spawn child sharing only stderr
spawn('prg', [], { stdio: ['pipe', 'pipe', process.stderr] });

// Open an extra fd=4, to interact with programs presenting a
// startd-style interface.
spawn('prg', [], { stdio: ['pipe', null, null, null, 'pipe'] });

It is worth noting that when an IPC channel is established between the parent and child processes, and the child is a Node.js process, the child is launched with the IPC channel unreferenced (using unref()) until the child registers an event handler for the 'disconnect' event or the 'message' event. This allows the child to exit normally without the process being held open by the open IPC channel.

On UNIX-like operating systems, the child_process.spawn() method performs memory operations synchronously before decoupling the event loop from the child. Applications with a large memory footprint may find frequent child_process.spawn() calls to be a bottleneck. For more information, see V8 issue 7381.

See also: child_process.exec() and child_process.fork().