Retain color when child_process.spawn is executed

I am trying to execute the windows command through cmd.exe in node.js using child_process.spawn. It runs correctly, but displays only the default text color. How to keep color. Is it possible?

var spawn = require('child_process').spawn, cmd = spawn('cmd', ['/s', '/c', 'C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\MSBuild c:\\test.sln']); cmd.stdout.on('data', function(data){ process.stdout.write(data); }); cmd.stderr.on('data', function(data){ process.stderr.write(data); }); cmd.on('exit', function(code){ console.log(code); }); 

When executed through node, the color is not saved. Executing via node.js

When executed directly through cmd.exe, color is present. (This is the expected behavior). How do I get this behavior when executed through node. When executing through cmd.exe

+47
Oct 11 '11 at 12:08
source share
6 answers

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 sure if customFds on Windows. I know that old obsolete functions do not work, but when only passing [0,1,2] as fd, I think there is a special case for this.

I did something similar here , but I only ran this command on Unix machines. So let me know if this works on Windows.

+7
Dec 29 '11 at 18:45
source share

There is a new option 'stdio' for child_process.spawn (). Try the following:

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

β€œInherit” means [0, 1, 2] or [process.stdin, process.stdout, process.stderr].

+87
Jan 09 '13 at 9:03 on
source share

The cross-platform 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

+3
May 13 '16 at 15:44
source share

If you get an error:

Unable to call 'on' null method

Try the following:

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

works with mocha

+3
Apr 12 '17 at 16:47 on
source share

This does not fix the underlying problem (lack of proper TTY flow), but it should help get around it.

If the subprocess that you use uses color support ( https://www.npmjs.com/package/supports-color ) as chalk, then you can set the FORCE_COLOR variable to any value and it will skip the rest of the checks. This will allow you to continue to use pipes (and capture / modify the returned data) as opposed to the inherit fix.

There is also a node -pty module ( https://www.npmjs.com/package/node-pty ) that provides .spawn ability to pass pty (pseudo tty), which can be a more holistic answer. I have not played with him yet, and I'm not sure if this is a cross platform or not.

+2
Mar 16 '17 at 16:28
source share

If you want to keep the color or add some notation for output, you can try the code below:

 var spawn = require('child_process').spawn, cmd = spawn('cmd', ['/s', '/c', 'C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\MSBuild c:\\test.sln'], stdio: [process.stdin, process.stdout, 'pipe']); var customStream = new stream.Writable(); customStream._write = function (data, ...argv) { console.log('your notation'); process.stderr._write(data, ...argv); }; cmd.stderr.pipe(customStream); 

Please note that the code uses es6

+1
Jan 04 '17 at 4:32 on
source share



All Articles