Nodejs child_process.exec : Disable printing of stdout on console

Try this:

var exec = require('child_process').exec;
exec('identify -verbose '+originalFilePath, { stdio: ['pipe', 'pipe', 'ignore']}, function(err,stdout,stderr){
    var strOut = stdout;    //  Do something with stdout
});

This ignores the stderr from the exec command but still displays errors and the normal output. See the doc for further configurations.


execSync('echo "hello"', { stdio: [] });

In your exact situation, my best solution was to set stdio to 'pipe'. This gives exactly what you'd like.

const execSync = require('child_process').execSync;
try {
  let options = {stdio : 'pipe' };
  let stdout = execSync('echo hello' , options);
  console.log("I got success: " + stdout);
  execSync('rmdir doesntexist' , options);//will exit failure and give stderr
} catch (e) {
  console.error("I got error: " + e.stderr ) ;
}

Result:

I got success: hello
I got error: rmdir: doesntexist: No such file or directory

Notice: Child process is silent

Nothing was printed to console by the child processes themselves, yet we get full stdout and stderr messages to work with.

This is at odds with the documentation* which states that pipe is the default configuration. In reality, setting stdio to pipe changes the behaviour.


* https://nodejs.org/api/child_process.html#child_process_options_stdio