Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible for child processes in Node.js to preserve colored output?

Tags:

node.js

I am writing my first Node.js command line tool using Liftoff. One of the important steps in my application is to copy some files and folders to the user's cwd. I am using exeq to accomplish this. It runs a series of commands:

  • cd app - cd to cwd/app/ where the gulpfile.js and package.json reside
  • npm install - install dependencies
  • gulp - run gulp

Functionally, exeq does exactly what I want it to do. It executes those three commands in sequence and does so successfully. My problem is that exeq does not preserve colored output, so the logs from npm install and gulp are plain white text, making them very difficult to parse.

My research thus far has not turned up an alternative node package that does the job nor a clear method for preserving colored output with my current setup. I did get a lead from someone that this may be a problem with the environment and that I need a way to tell exeq it's in an environment that supports colored output. Unfortunately, exeq doesn't appear to have any options or arguments, so I have no idea how to go about doing that.

Is this a limitation of node child processes, or is there a way to preserve the colored output?

like image 400
pfist Avatar asked Jul 24 '15 21:07

pfist


People also ask

How does node js handle child processes?

Usually, Node. js allows single-threaded, non-blocking performance but running a single thread in a CPU cannot handle increasing workload hence the child_process module can be used to spawn child processes. The child processes communicate with each other using a built-in messaging system.

Why do you use forever with node js?

The purpose of Forever is to keep a child process (such as your node. js web server) running continuously and automatically restart it when it exits unexpectedly.

Could we run an external process with node js?

To execute an external program from within Node. js, we can use the child_process module's exec method. const { exec } = require('child_process'); exec(command, (error, stdout, stderr) => { console.

Is node js a runtime environment?

Node. js is a JavaScript runtime environment that achieves low latency and high throughput by taking a “non-blocking” approach to serving requests. In other words, Node. js wastes no time or resources on waiting for I/O requests to return.


1 Answers

So gulp for instance uses a module called chalk to log formatted output. chalk in turn, uses a module called supports-color which does the actual terminal type detection. When chalk is require()d, it automatically uses supports-color to determine how many colors are available.

Ordinarily, supports-color will report that no colors are available when the process is executed as a child process with the default stdio options, since stdout is not a tty in that case, it is a pipe. Fortunately though, supports-colors provides a couple of options to override that check:

  • supports-colors uses a module called has-flag to look for process.argv entries like --color, --colors, etc. to force basic (16) color support. You can also use --color=256 to force 256 colors and arguments like --color=full to force true color mode (16 million colors). So for instance you'd supposedly call gulp like gulp --colors to get basic color output.

  • supports-colors also checks for an environment variable called FORCE_COLOR, which will force basic color support if it is otherwise detected that no colors are supported.

For npm, you can force color output a couple of different ways. Append the --color always command-line argument or set NPM_CONFIG_COLOR=always in the environment (you can do this by setting env in the options passed to child_process.exec()/child_process.spawn()).

like image 71
mscdex Avatar answered Sep 19 '22 00:09

mscdex