preserve color when executing child_process.spawn

If you are getting error:

Cannot call method 'on' of null

Try this:

spawn("command", ["args"], { env : { FORCE_COLOR: true }});

works with mocha


crossplatform solution that worked for me was to use both shell: true and stdio: 'inherit':

const spawn = require('child_process').spawn;

spawn('node', ['./child.js'], { shell: true, stdio: 'inherit' });

thanks @59naga https://github.com/nodejs/node/issues/2333


Try this instead:

var spawn = require('child_process').spawn
  , command = 'C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\MSBuild c:\\test.sln'
  , cmd    = spawn('cmd', ['/s', '/c', command], { customFds: [0,1,2] });

cmd.on('exit', function(code){
    console.log(code);
});

Note that I'm not positive whether or not customFds works on Windows. I know that it's old deprecated functionality doesn't work, but when only passing [0,1,2] as the fd's, I think there is a special case for that.

I've been doing something similar here, but I've only ran that command on Unix machines. So let me know if that works on Windows.


There are new 'stdio' option for child_process.spawn(). Try following:

spawn("path to executable", ["params"], {stdio: "inherit"});

"Inherit" means [0, 1, 2] or [process.stdin, process.stdout, process.stderr].

Tags:

Node.Js