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标志。这对于在子进程的标准输入输出句柄上进行重叠 I/O 是必要的。更多详情请参阅 文档。在非 Windows 系统上,这与'pipe'完全相同。 -
'ipc':创建一个 IPC 通道,用于在父进程和子进程之间传递消息/文件描述符。一个ChildProcess最多可以有一个 IPC stdio 文件描述符。设置此选项将启用subprocess.send()方法。如果子进程是一个 Node.js 实例,IPC 通道的存在将启用process.send()和process.disconnect()方法,以及子进程中的'disconnect'和'message'事件。以除
process.send()之外的任何方式访问 IPC 通道 fd,或将 IPC 通道与非 Node.js 实例的子进程一起使用,是不受支持的。 -
'ignore':指示 Node.js 在子进程中忽略 fd。虽然 Node.js 总是会为它生成的进程打开 fds 0、1 和 2,但将 fd 设置为'ignore'会导致 Node.js 打开/dev/null并将其附加到子进程的 fd。 -
'inherit':通过相应的 stdio 流传递到/来自父进程。在前三个位置中,这分别相当于process.stdin、process.stdout和process.stderr。在其他位置,则相当于'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. The stream passed in thestdinposition is the source from which the child process reads its input, and the streams in thestdout/stderrpositions receive the output the child writes. This is the opposite ofsubprocess.stdin(writable) andsubprocess.stdout(readable), which are the parent's ends of the pipes created by'pipe'. -
正整数:整数值被解释为父进程中打开的文件描述符。它会与子进程共享,类似于 <Stream> 对象的共享方式。在 Windows 上不支持传递套接字。
-
null、undefined:使用默认值。对于标准输入输出文件描述符 0、1 和 2(也就是 stdin、stdout 和 stderr),会创建一个管道。对于文件描述符 3 及以上,默认值是'ignore'。
const { spawn } = require('node: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'] });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().