9

我认为这很简单,但以下内容无法按预期工作。

我想将数据wc从 Node.

文档和其他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 })

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

选项“stdio”的值“Readable { ... } 无效

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

4

1 回答 1

10

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

spawnSync('wc', ['-c'], { input : 'one two three' })
于 2017-07-04T12:45:25.903 回答