Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change node.js's console font color?

I had to change the console background color to white because of eye problems, but the font is gray colored and it makes the messages unreadable. How can I change it?

like image 811
MaiaVictor Avatar asked Mar 20 '12 04:03

MaiaVictor


People also ask

How to change Node js console font color?

For example if you want to have a Dim, Red text with Blue background you can do it in Javascript like this: console. log("\x1b[2m", "\x1b[31m", "\x1b[44m", "Sample Text", "\x1b[0m");

How do I change the color of my console log?

Add CSS Styles to Console Log Output log('%c hello world ', 'background: #222; color: #bada55'); We add the %c tag so that we can apply the CSS styles in the 2nd argument to the hello world text. We set the background property to set the background color. And we set the color property to change the text color.

How to change font style in Node js?

You can use the chalk module to customize the statements printed on the console.By using it, one can change the text font-color to any color. One can also change the font-style to Bold, Italic or Underlined. Also, you can highlight the printed text.

What is node color?

The Color node is a basic node that lets you change the color of your objects at any time in a non-destructive way. It only outputs color and does not have any inputs. Default Color. Color set to brown. Color set to pink with.


2 Answers

Below you can find colors reference of text to command when running node.js application:

console.log('\x1b[36m%s\x1b[0m', 'I am cyan');  //cyan console.log('\x1b[33m%s\x1b[0m', stringToMakeYellow);  //yellow 

Note %s is where in the string (the second argument) gets injected. \x1b[0m resets the terminal color so it doesn't continue to be the chosen color anymore after this point.

Colors reference

Reset = "\x1b[0m" Bright = "\x1b[1m" Dim = "\x1b[2m" Underscore = "\x1b[4m" Blink = "\x1b[5m" Reverse = "\x1b[7m" Hidden = "\x1b[8m"  FgBlack = "\x1b[30m" FgRed = "\x1b[31m" FgGreen = "\x1b[32m" FgYellow = "\x1b[33m" FgBlue = "\x1b[34m" FgMagenta = "\x1b[35m" FgCyan = "\x1b[36m" FgWhite = "\x1b[37m"  BgBlack = "\x1b[40m" BgRed = "\x1b[41m" BgGreen = "\x1b[42m" BgYellow = "\x1b[43m" BgBlue = "\x1b[44m" BgMagenta = "\x1b[45m" BgCyan = "\x1b[46m" BgWhite = "\x1b[47m" 

EDIT:

For example, \x1b[31m is an escape sequence that will be intercepted by your terminal and instructs it to switch to the red color. In fact, \x1b is the code for the non-printable control character escape. Escape sequences dealing only with colors and styles are also known as ANSI escape code and are standardized, so therefore they (should) work on any platform.

Wikipedia has a nice comparison of how different terminals display colors https://en.wikipedia.org/wiki/ANSI_escape_code#Colors

like image 95
Bud Damyanov Avatar answered Oct 01 '22 01:10

Bud Damyanov


There are multiple packages available for formatting console text in Node.js. The most popular are:

  • chalkGitHub Repo stars

  • cli-colorGitHub Repo stars

  • colorsGitHub Repo stars > EDIT: colors no longer recommended as it has denial of service vulnerability see: https://snyk.io/blog/open-source-npm-packages-colors-faker/ for details

Usage:

CHALK:

const chalk = require('chalk'); console.log(chalk.red('Text in red')); 

CLI-COLOR:

const clc = require('cli-color'); console.log(clc.red('Text in red')); 

like image 42
nelsonic Avatar answered Oct 01 '22 02:10

nelsonic