Welcome to the Linux Foundation Forum!

child_process.execSync stderr printing in terminal instead of returning as result.

I was trying out the child_process.execSync examples and it appears that doing a console.error prints directly in the console instead of returning it as a result. I am running this on Node 14 on MacOS. Is that the expected behavior?

"use strict";
const { execSync } = require("child_process");

const output = execSync(`node -e "console.error('subprocess stdio error')"`);
console.log(output)

Comments

  • Hey @seetdev

    -e evaluates only, -p prints.

    Your console.log(output) line will be an empty buffer, (<Buffer >) because the child process had no output on STDOUT (execSync only returns STDOUT) .

    The console.error line prints to STDERR in the child process, by default the the child process STDERR shares the parent STDERR so you're just writing to the parent STDERR in the child process which is why you see the output (you can remove console.log(output) and you will see still your logged line)

    All of this is completely expected behaviour.

  • Thanks and we can close this as well

  • thanks @seetdev !

This discussion has been closed.

Categories

Upcoming Training