1043

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?

5
  • 4
    In the same place you already used to change the background color, you can change the other colors. Commented Mar 20, 2012 at 20:44
  • 1
    I'm having the same problem. I suspect @Viclib is using windows (as am I), which is why instructions to change terminal colors are a foreign concept. The windows command prompt allows changing 2 foreground and 2 background colors. Node uses other colors which windows command prompt cannot define. Commented Apr 7, 2015 at 9:25
  • @GregWoods. the accepted answer below does work in Windows ! Commented Dec 15, 2018 at 15:38
  • 2
    I later discovered that my mental model for how Windows command prompt colours worked, was completely wrong. I assumed incorrectly (due to a terrible UI) that you can only change foreground, background colours. This is wrong. All 16 colours can be used by a console app, and it is vital to pick sensible colours for all 16, and to ALWAYS use colour tile 1 as background (and tile 9 for "popup background"). This was such a revelation to me, I wrote a blog post (a rare event indeed). gregwoods.co.uk/2015/04/… Commented Dec 20, 2018 at 14:29
  • @GregWoods That blog post link is dead now Commented May 21, 2022 at 14:54

43 Answers 43

1
2
3

With Node v21.7.0, a native styleText has been added: https://nodejs.org/en/blog/release/v21.7.0. And later backported to 20.12.0.

Example:

import { styleText } from 'node:util';

console.log(styleText('red', 'Error! Error!'));

Discovered thanks to https://pawelgrzybek.com/node-js-added-utility-for-text-formatting-you-may-not-need-chalk-anymore/

Sign up to request clarification or add additional context in comments.

Comments

2

2017:

Simple way, adding time color to the message, you don't need to change your code, use keep your console.log('msg') or console.err('error')

var clc = require("cli-color");
var mapping = {
  log: clc.blue,
  warn: clc.yellow,
  error: clc.red
};

["log", "warn", "error"].forEach(function(method) {
  var oldMethod = console[method].bind(console);
  console[method] = function() {
    oldMethod.apply(
      console,
      [mapping[method](new Date().toISOString())]
      .concat(arguments)
    );
  };
});

enter image description here

Comments

2
var to_rgb = function (_text, _r, _g, _b) {
    return "\x1b[38;2;" + _r + ";" + _g + ";" + _b + "m" + _text + "\x1b[0m";
};

This code help set foreground color: \x1b[38;2;R;G;Bm

This may not work some places

Comments

2

Minimal aliases:

{
  const f = (color) => (...args) => {
    for (const x of [color, ...args, "\33[0m"]) console.log(x);
  };

  Object.assign(console, {
    black: f("\33[30m"),
    red: f("\33[31m"),
    green: f("\33[32m"),
    yellow: f("\33[33m"),
    blue: f("\33[34m"),
    magenta: f("\33[35m"),
    cyan: f("\33[36m"),
    white: f("\33[37m"),
  });
}

// Usage
console.blue("Blue world");

Comments

2

A summary of console color manipulation

Colors are defined by non-printable sequence (Esc codes). They are called Escape codes because they start with Esc (\033 or \x1b).

[0m - reset colors
[X;3Ym - foreground colors
[X;4Ym - background colors
[X;9Ym - bright foreground colors
[X;10Ym - bright background colors
[X;38;<spec>m - foreground custom colors
[X;48;<spec>m - background custom colors

Where:
 X is style, 0 reset, 1 bold or bright, 3 cursive, 4 underline, 5 slow blink, 6 fast blink, 7 reverse, 8 hide, 9 crossed-out
 Y is color, 0 black, 1 red, 2 green, 3 yellow, 4 blue, 5 magenta, 6 cyan, 7 white

Where <spec>: 2;R;G;B or 5;N
Where N is a 8-bit 256 color sequence: 0 <= N <= 255

    0-7:  standard colors (as in ESC [ 30–37 m)
   8-15:  high intensity colors (as in ESC [ 90–97 m)
 16-231:  6 × 6 × 6 cube (216 colors): 16 + 36 × r + 6 × g + b (0 ≤ r, g, b ≤ 5)
232-255:  grayscale from dark to light in 24 steps

0 black      8 dark grey
1 red        9 bright
2 green     10 bright
3 yellow    11 bright
4 blue      12 bright
5 magenta   13 bright
6 cyan      14 bright
7 grey      15 white

Range 16-255 has 6 grades for red, green, blue
where 16 - black, 17 - dark blue, 196 - bright red, 255 - white
Range 232-255 greyscale from black to white
Color sequence is calculated by looping B-G-R starting with 95 and step of 40
16 =   0,  0,   0
17 =   0,  0,  95 blue: most inner loop step 40
22 =   0,  95,  0 green: inner loop step 40
52 =  95,   0,  0 red: outer loop step 40

5 ways to define bright red

console.log('\33[38;2;255;0;0m%s\33[0m','Hello')
console.log('\33[38;5;9m%s\33[0m','Hello')
console.log('\33[38;5;196m%s\33[0m','Hello')
console.log('\33[91m%s\33[0m','Hello')
console.log('\33[1;31m%s\33[0m','Hello')

Comments

1

If you are using Windows CMD then go to the terminal Properties/Colors (CMD top left) and then redefine the RGB value of the offensive color. In my case I believe it's the fifth color square from the left, which I changed to (222,222,222). It does not matter if the currently selected radio button shows Screen Text or Screen Background as you just redefine that specific "system" color. Once you changed the color don't forget to select back the preferred color for the background or text before clicking OK.

After the change all these reddish messages from Node (Ember in my case) are clearly visible.

Comments

1

In ubuntu you can simply use color codes:

var sys = require('sys');
process.stdout.write("x1B[31m" + your_message_in_red + "\x1B[0m\r\n");

Why the unused require?
It was a couple of years ago. It was neccessary for stdout write, maybe now it is already imported by default.
Ah, ok. I was just curious because it's not like sys was being used anywhere. It actually isn't necessary nowadays, though!
1

node-colorify

Provides functions to print texts in color and also to do text formatting such as bold, blink, etc..

While the link you provided may answer the question. It is best to put the essential parts of your solution directly in your answer in case the page at the link expires in the future.
1

I really liked @Daniel's answer, but the console.log{color} functions didn't work the same way as regular console.log. I have made a few changes, and now all parameters to the new functions will be passed to console.log (as well as the color codes).

const _colors = {
    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",
};

const enableColorLogging = function(){
    Object.keys(_colors).forEach(key => {
        console['log' + key] = function(){
            return console.log(_colors[key], ...arguments, _colors.Reset);
        }
    });
}

Comments

1

Inline typescript solution

export const color = (function (colors) {
    const fn = (code: number, str: string) => `\x1b[${code}m${str}\x1b[39m`;
    const obj = { grey: fn.bind(null, 90) };
    for (let i = 0; i < colors.length; i++) obj[colors[i]] = fn.bind(null, 30 + i);
    return obj as { [K in typeof colors[any] | 'grey']: (str: string) => string };
})(['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'] as const);

enter image description here

Comments

1

An alternative to all this is to use a simple ANSI code generator.

  • You don't need to install packages
  • No need to search color codes, just click the button

GIF DEMO

You can use it at https://console-colors.vercel.app/

Public repository: https://github.com/alecshoppe/console-colors

Comments

0

This is an approach for Windows 10 (maybe for 7) and it changes the color scheme (theme) for cmd, npm terminal itself, not only console output for a particular app.

I found the working Windows plugin - Color Tool, which is presumably developed under Windows umbrella. A description is available at the link.

I added colortool directory into system environment path variable and now it is available whenever I start terminal (NodeJs command prompt, cmd).

Comments

0

This works for the (I know of) Node console.

The package is shortcuts, and you can install it with this command. const short = require('@testgrandma/shortcuts');

There is two commands you can do to change the color. It's RGB color and Hex color short.colorRGB(r,g,b);

short.colorhex(hex);

You can do console.log(short.colorhex('d50000') + 'This is red!');

The package can be found here.

https://www.npmjs.com/package/@testgrandma/shortcuts

Comments

1
2

Your Answer

Draft saved
Draft discarded

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.