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..
You can't do it. Read @mattr's answer.
Instead, you can hook into the process.stdout.write
and get all the text printed to process.stdout
. I'm using the code below with my Node.js server.
global.serverLog = "";
process.stdout.write = (function(write) {
return function(string, encoding, fileDescriptor) {
global.serverLog += string;
write.apply(process.stdout, arguments);
};
})(process.stdout.write);
Note that you can do the same thing to process.stderr
, too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With