admin管理员组

文章数量:1419905

In this answer, the users describes in details how to color the text in the console when using node.js. The official documentation is even posted in a ment to the answer.

Unfortunately, this only shows us how to use 8 colors for the text, and the same 8 colors for the background. In practive, since any text will be invisible on the same background color, this means we can only use 7 colors unless we are willing to change the background often.

FgBlack = "\x1b[30m"
FgRed = "\x1b[31m"
FgGreen = "\x1b[32m"
FgYellow = "\x1b[33m"
FgBlue = "\x1b[34m"
FgMagenta = "\x1b[35m"
FgCyan = "\x1b[36m"
FgWhite = "\x1b[37m"

What I am looking for, is a way to get more colors for the console. It can be with an external module or library, can be official or not, etc.

Specifically, the colors Orange, Purple, Pink and Brown are very mon, and I assume that there is some way to get them.

Of course, the ideal situation would be some way to provide an RGB directly, so I can make my own shades of colors too, but I'll accept any answer that provides access to at least 4 more colors, because I need 11-12 at minimum for something I'm doing.

How can I get more colors for the console in Node.Js?

In this answer, the users describes in details how to color the text in the console when using node.js. The official documentation is even posted in a ment to the answer.

Unfortunately, this only shows us how to use 8 colors for the text, and the same 8 colors for the background. In practive, since any text will be invisible on the same background color, this means we can only use 7 colors unless we are willing to change the background often.

FgBlack = "\x1b[30m"
FgRed = "\x1b[31m"
FgGreen = "\x1b[32m"
FgYellow = "\x1b[33m"
FgBlue = "\x1b[34m"
FgMagenta = "\x1b[35m"
FgCyan = "\x1b[36m"
FgWhite = "\x1b[37m"

What I am looking for, is a way to get more colors for the console. It can be with an external module or library, can be official or not, etc.

Specifically, the colors Orange, Purple, Pink and Brown are very mon, and I assume that there is some way to get them.

Of course, the ideal situation would be some way to provide an RGB directly, so I can make my own shades of colors too, but I'll accept any answer that provides access to at least 4 more colors, because I need 11-12 at minimum for something I'm doing.

How can I get more colors for the console in Node.Js?

Share Improve this question edited Sep 6, 2018 at 12:53 Luca Kiebel 10.1k7 gold badges32 silver badges46 bronze badges asked Aug 31, 2018 at 17:28 Kaito KidKaito Kid 1,1135 gold badges17 silver badges38 bronze badges 4
  • Have you tried chalk? They (used to) support Truecolor – Luca Kiebel Commented Aug 31, 2018 at 17:33
  • I just tried it and it works, why didn't you put this as an answer? It does exactly what I wanted :D – Kaito Kid Commented Sep 6, 2018 at 12:36
  • I really thought we could have developed some sort of discussion on the topic here. Like you saying "No, but I'll try it now", and then I would've wrote an answer.... – Luca Kiebel Commented Sep 6, 2018 at 12:37
  • Hope it's still relevant ;-) – Luca Kiebel Commented Sep 6, 2018 at 12:51
Add a ment  | 

3 Answers 3

Reset to default 2

You can use chalk for this:

First, make sure that you enable Truecolor for chalk, so that you can use all the colors you want to use:

const chalk = require("chalk"),
      ctx = new chalk.constructor({level: 3}); // 3 for Truecolor: https://github./chalk/chalk#chalklevel

After that you can use the Extended Colors from CSS, like Orange, Purple, Pink and Brown:

console.log(ctx.keyword('orange')('Orange!'))
console.log(ctx.keyword('purple')('Purple!'))
console.log(ctx.keyword('pink')('Pink!'))
console.log(ctx.keyword('brown')('Brown 

本文标签: javascriptHow can I get more colors for nodejs consoleStack Overflow