mcuong mcuong - 6 months ago 27
Node.js Question

Nodejs v6: exec "closure compiler" stdout always empty

I try to exec CC from nodejs:

// already tried exec, spawn
process.execFile('java', [
'-jar', 'CC-path', '-js', 'js-path', 'other-options'
],
(error, stdout) => {
if (error != null) {
throw error // if there is any errors then will showed in terminal
}
console.log(stdout.toString()) // but stdout always empty when no errors exist
}
)
// I have tested by manual running the command in terminal and got output:
// "...many lines before"
// 0 error(s), 10 warning(s), 74.2% typed


How can I get all "Nodejs exec" output like when I manual type the command in terminal?

Answer

.execFile() takes a callback that gets three parameters (err, stdout, stderr). You should log all three to see what is going on. My guess is that there is something showing on stderr which you are ignoring. You also aren't logging err if there's something there.

process.execFile('java', [
        '-jar', 'CC-path', '-js', 'js-path', 'other-options'
    ], (error, stdout, stderr) => {
        if (error) {
            console.log(error);
            return;
        }
        console.log("stdout: ", stdout);
        console.log("stderr: ", stderr);
    }

Also, your throw error will likely do nothing useful because doing a throw from within a regular async callback just throws into the bowels of the child_process library, not any where that you can ever catch it.

Comments