options.stdio
options.stdio 选项用于配置在父进程和子进程之间建立的管道。默认情况下,子进程的 stdin、stdout 和 stderr 会重定向到 ChildProcess 对象上的相应 subprocess.stdin、subprocess.stdout 和 subprocess.stderr 流。这等同于将 options.stdio 设置为 ['pipe', 'pipe', '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'].】
为了方便,options.stdio 可以是以下字符串之一:
【For convenience, options.stdio may be one of the following strings:】
'pipe':相当于['pipe', 'pipe', 'pipe'](默认值)'overlapped':等同于['overlapped', 'overlapped', 'overlapped']'ignore':等同于['ignore', 'ignore', 'ignore']'inherit':相当于['inherit', 'inherit', 'inherit']或[0, 1, 2]
否则,options.stdio 的值是一个数组,其中每个索引对应子进程中的一个文件描述符(fd)。fd 0、1 和 2 分别对应标准输入(stdin)、标准输出(stdout)和标准错误(stderr)。可以指定额外的文件描述符以在父进程和子进程之间创建额外的管道。其值可以是以下之一:
【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:】
-
'pipe':在子进程和父进程之间创建管道。管道的父端通过child_process对象上的subprocess.stdio[fd]属性暴露给父进程。为文件描述符 0、1 和 2 创建的管道也分别可作为subprocess.stdin、subprocess.stdout和subprocess.stderr使用。这些并不是真正的 Unix 管道,因此子进程不能通过它们的描述符文件使用它们,例如/dev/fd/2或/dev/stdout。 -
'overlapped':与'pipe'相同,不同之处在于句柄上设置了FILE_FLAG_OVERLAPPED标志。这对于子进程的 stdio 句柄进行重叠 I/O 是必要的。有关更多详细信息,请参见 文档。在非 Windows 系统上,这与'pipe'完全相同。 -
'ipc':创建一个 IPC 通道,用于在父进程和子进程之间传递消息/文件描述符。一个ChildProcess最多可以有一个 IPC 标准输入输出文件描述符。设置此选项将启用subprocess.send()方法。如果子进程是 Node.js 实例,IPC 通道的存在将启用process.send()和process.disconnect()方法,以及子进程中的'disconnect'和'message'事件。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. -
'ignore': Instructs Node.js to ignore the fd in the child. While Node.js will always open fds 0, 1, and 2 for the processes it spawns, setting the fd to'ignore'will cause Node.js to open/dev/nulland attach it to the child's fd. -
'inherit': Pass through the corresponding stdio stream to/from the parent process. In the first three positions, this is equivalent toprocess.stdin,process.stdout, andprocess.stderr, respectively. In any other position, equivalent to'ignore'. -
<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
stdioarray. The stream must have an underlying descriptor (file streams do not start until the'open'event has occurred). NOTE: While it is technically possible to passstdinas a writable orstdout/stderras readable, it is not recommended. Readable and writable streams are designed with distinct behaviors, and using them incorrectly (e.g., passing a readable stream where a writable stream is expected) can lead to unexpected results or errors. This practice is discouraged as it may result in undefined behavior or dropped callbacks if the stream encounters errors. Always ensure thatstdinis used as readable andstdout/stderras writable to maintain the intended flow of data between the parent and child processes. -
Positive integer: The integer value is interpreted as a file descriptor that is open in the parent process. It is shared with the child process, similar to how <Stream> objects can be shared. Passing sockets is not supported on Windows.
-
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('node:child_process');
const process = require('node: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'] });import { spawn } from 'node:child_process';
import process from 'node: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'] });值得注意的是,当在父进程和子进程之间建立了 IPC 通道,而子进程是一个 Node.js 实例时,子进程会以未引用(使用 unref())的方式启动 IPC 通道,直到子进程为 'disconnect' 事件或 'message' 事件注册事件处理程序为止。这允许子进程正常退出,而不会因为打开的 IPC 通道而导致进程保持运行。
另请参见:child_process.exec() 和 child_process.fork()。
【It is worth noting that when an IPC channel is established between the
parent and child processes, and the child process is a Node.js instance,
the child process is launched with the IPC channel unreferenced (using
unref()) until the child process registers an event handler for the
'disconnect' event or the 'message' event. This allows the
child process to exit normally without the process being held open by the
open IPC channel.
See also: child_process.exec() and child_process.fork().】