1

I'm running require('child_process').exec('npm install') as a child process in a node.js script, but I want it to retain console colors. I'm running in windows, but want this script to be portable (e.g. to linux). How do I start a process that think's it's being run from the console?

Note: I'd rather not have npm-specific answers, but an answer that allows me to trick any command.

B T
  • 46,771
  • 31
  • 164
  • 191

1 Answers1

3

You can do this by letting the child process inherit the master process' stdio streams. This means you need to user spawn rather than exec, and this what you'd do:

var spawn = require('child_process').spawn;
var child = spawn('npm', ['install'], {
  stdio: 'inherit'
});
hexacyanide
  • 76,426
  • 29
  • 148
  • 154
  • doing this seems to give me an ENOENT error on windows. After reading this, it seems the problem is that windows commandline recognizes npm as the file npm.cmd, whereas spawn does not: http://stackoverflow.com/questions/17516772/using-nodejss-spawn-causes-unknown-option-and-error-spawn-enoent-err – B T Sep 18 '13 at 22:23
  • Also, this answer doesn't answer my question - how to trick it into thinking its being run from the commandline (so it'll do things like add color). This answer did help and works for my case, because I just want to print out all the output (instead of sending it somewhere else) so ima give it +1, but I want a better answer. – B T Sep 18 '13 at 22:25
  • I think most applications check if the `stdio` streams represent a TTY, but you don't want an interactive terminal, do you? – hexacyanide Sep 19 '13 at 00:39
  • How do applications check for that kind of thing? I don't know how to decide if I do or don't want an interactive terminal. What are the considerations around that? – B T Sep 19 '13 at 04:40