user1506145 user1506145 - 2 months ago 8
Node.js Question

NodeJS parse process.stdout to a variable

I am trying to read from stdout and put it to a variable in NodeJS. This is what I've tried:

process.stdout.on('data', function(data) {
console.log(data.toString())
});

console.log('hello')


I expect it to output hello two times, but it only outputs it one time. Why is the callback not triggered? It works when using stdin..

Answer

You are trying to use the Readable Stream API on a Writable Stream. Specifically, the process.stdout stream is for writing to stdout not reading from stdout. What you are trying to do isn't possible, would create an infinite loop if it did work, and breaks the definition of what stdin and stdout are.