Home > OS >  Change text color in console
Change text color in console

Time:12-12

I want to change the console text color in my node console (command prompt) when using console.info() or console.error(). I know you can change text color with some backslashes and color codes, but is there a way to make it default some colors? Like console.error() could default to red, and console.warn() could default to yellow. Any help is appreciated, thanks!

CodePudding user response:

I suppose you could overwrite those console functions with your own, with your desired color:

// Save a reference to the original functions
const { info, error } = console;
console.info = (arg) => {
  info.call(console, `\x1b[33m${arg}\x1b[0m`);
};
console.error = (arg) => {
  info.call(console, `\x1b[31m${arg}\x1b[0m`);
};

And so on. Use whatever colors you want.

  • Related