'Pipe spawned process stdout to function on flush

My goal is to spawn another binary file in a child process, then handle the stdout on a line-by-line basis (And do some processing against that line). For testing I'm using Node. To do this I tried a readable & writable stream but it a type error is throwed saying "The argument 'stdio' is invalid. Received Writable"

const rs = new stream.Readable();
const ws = new stream.Writable();

const child = cp.spawn("node", [], {
    stdio: [process.stdin, ws, process.stderr]
})

let count = 0;
ws.on("data", (data) => {
    console.log(data, count)
});

Anyone have any ideas?



Solution 1:[1]

One way is to use the stdio streams returned by spawn and manually pipe them:

const child = cp.spawn("node", [])

child.stdin.pipe(process.stdin)
child.stdout.pipe(ws)
child.stderr.pipe(process.stderr)

let count = 0;
ws.on("data", (data) => {
    console.log(data, count)
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 programmerRaj