Node.js将文本作为"spawnSync"的标准输入传递

Bri*_*unt 7 node.js node-streams

我认为这很简单,但以下内容不能按预期工作.

我想wc从Node 管道数据到一个进程,比如说(只是一个用于说明的任意命令).

文档和其他SO问题似乎表明传递流应该工作:

const {spawnSync} = require('child_process')
const {Readable} = require('stream')

const textStream = new Readable()
textStream.push("one two three")
textStream.push(null)

const stdio = [textStream, process.stdout, process.stderr]
spawnSync('wc', ["-c"], { stdio })
Run Code Online (Sandbox Code Playgroud)

不幸的是,这会引发错误:

值"可读{...}对选项"stdio"无效

代码相关的位由internal/child_process.js不立即揭示了什么预期的有效选项.

rob*_*lep 8

要将特定数据stdin显示为子进程的数据,可以使用以下input选项:

spawnSync('wc', ['-c'], { input : 'one two three' })
Run Code Online (Sandbox Code Playgroud)