Search code examples
javascriptnode.jsmacosprocessspawn

Unable to get output from child process


So, I have a simple app (I have written) which writes to stdout.

When I run it by itself, the output is being printed fine.

Now, when I spawn the process with node... nothing happens (apart from a message saying the process has finished)

Here's my code:

var spawn = require('child_process').spawn;
helper = spawn("myApp", []);
helper.stdout.on('data', function(data) { 
    console.log("GOT: " + data);
});
helper.stdout.on('end', function(data) {
    console.log("FINISHED: " + data);
});
helper.on('exit', function(code) {
    console.log("CLOSED!");
});

And here's the output:

FINISHED: undefined
CLOSED!

Yep, just that.

What's going on? Am I missing something?


Solution

  • Make sure your stdio output buffers are being flushed in your application:

      fflush(stdout); // or something like that
    

    Output straight to the tty has less buffering than output to a pipe.