Node.js child processes
me: Hey node, how can I tell if my child_process.spawn()
call failed before it even runs the desired command? Is there a return value? An exception? Maybe there’s an error event I can listen for?
node: Even better! Here’s an example from the documentation to do exactly that:
var spawn = require('child_process').spawn,
child = spawn('bad_command');
child.stderr.setEncoding('utf8');
child.stderr.on('data', function (data) {
if (/^execvp\(\)/.test(data)) {
console.log('Failed to start child process.');
}
});
me: Thanks, that’s… That’s just stunning.
(For extra points, this doesn’t even work if the child process inherits stderr
, as my actual code does. Is this just a silly example from the documentation, or is there really nothing better?)