Node.js将文本作为`spawnSync`的标准input

我认为这将是简单的,但以下不按预期工作。

我想把数据传递给一个进程,从节点上说(只是一个任意的例子) wc

文档和其他SO问题似乎表明,通过一个Stream应该工作:

 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 }) 

不幸的是这会抛出一个错误:

值“Readable {…}”对于选项“stdio”无效

来自internal/child_process.js的相关代码不会立即显示预期的有效选项。

要将特定数据作为subprocess的stdin数据提供,可以使用input选项:

 spawnSync('wc', ['-c'], { input : 'one two three' })