Node: Read spwn stderr / stdout after fast exit event?

I am using Node spawn

to create a new process. Sometimes this process will exit very quickly with an error code and stderr message. It looks like stderr is getting lost in this quick turn. I've tried this:

    reader.stderr.on('data', function (buf) {
        console.log('stderr message: ' + buf);
    });

    reader.on('exit', function (code, signal) {
        console.log('Exit');
    });

      

Output:

   Exit
   stderr message: ERROR: Missing required option for command.

      

I also tried reading it in a listener exit

but no luck:

     reader.on('exit', function (code, signal) {
        console.log('Exit');
        console.log('stderr: ' + reader.stderr.read());
    });

      

Output:

    Exit
    stderr: null

      

So, it looks like the problem is that the stderr output is too slow and ends after the exit event, where I need this information. How can I fix this?

+3


source to share


1 answer


Taken from the child_process docs forexit

:

Note that the stdio streams of the child process can be opened.

Then they describe the event close

:



This event is fired when the stdio streams of the child process have finished. This is different from "quitting" because multiple processes can use the same stdio streams.

So, it looks like you should be using close

, not exit

.

+2


source







All Articles