Node.js pass text as stdin `spawnSync`

I would have thought it would be simple, but the following doesn't work as expected.

I want to pass data to a process, say (just an arbitrary command for illustration) wc

, from Node.

The docs and other SO questions seem to indicate that streaming should work:

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

      

Unfortunately this throws an error:

The value "Readable {...} is invalid for option" stdio "

the corresponding bit of code frominternal/child_process.js

does not immediately show what the expected valid parameters are.

+3


source to share


1 answer


To represent specific data as stdin

data for a child process, you can use the option input

:



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

      

+3


source







All Articles