I am trying to buffer the results from my phantomjs script to nodejs but for some reason the buffer results is constantly coming out of STDERR but not STDOUT even though there doesn't seem to be an error.
NodeJS code:
var exec = require('child_process').exec;
exec('phantomjs console.js', function(stdout, stderr, error){
if (error !== null) {
console.log('exec error: ' + error);
}
console.log("Standard Output: " + stdout + " ");
console.log("Error Output: " + stderr + " ");
});
PhantomJS code (console.js):
console.log("Hello world!");
phantom.exit();
Result that I get:
exec error:
Standard Output: null
Error Output: Hello world!
As per Dan Davis, you have error, stderr, and stdout switched - not the console.log statements, but the order of the parameters in the function. Below is the correct order as per the docs
var exec = require('child_process').exec;
exec('phantomjs console.js', function(error, stdout, stderr){
if (error !== null) {
console.log('exec error: ' + error);
}
console.log("Standard Output: " + stdout + " ");
console.log("Error Output: " + stderr + " ");
});
The reason for your output was that your stdout was where the actual error should have been, so it was empty. Your stderr was in the position of the actual stdout so it recieved the output. Your error was in the position of the actual stderr, so it was empty.