Search code examples
node.jschild-processmongodump

node child_process gives valid output in stderr for mongodump


I'm executing mongodump command from nodejs child_process.

I have tried both exec and spawn, but the progress printed by mongodump is hitting the data event of stderr instead of stdout

var exec = require('child_process').exec,
    ls    = exec('mongodump --gzip --archive="/home/test-machine/test.archive" --db myDB');

ls.stdout.on('data', function (data) {
  console.log('stdout: ' + data.toString());
});

ls.stderr.on('data', function (data) {
  console.log('stderr: ' + data.toString());
});

ls.on('exit', function (code) {
  console.log('child process exited with code ' + code.toString());
});

The mongodump is not crashing, or throwing any error, its executing as it should. But somehow nodejs is treating the output as stderr


Solution

  • mongodump writes log messages to stderr, which isn't very uncommon, although it makes parsing the output for errors a bit harder because you need to manually filter out the error messages.