When making any kind of node.js project that may involve output to the command line interface, it may be desired to style that output, for the sake of adding emphases, or just to make it look nice. Many CLI tools make use of color, for one reason or another, so if you want to add color to the output of you node.js CLI tools, you might want to check out chalk.
When I first wrote this post back in may I was using chalk 1.1.3, but as of this writing the latest version is now 2.3.0 as such there are a few new features, and some things just still work the same as aways.
Getting started with chalk is real simple, after adding it to your project with the usual
Throwing together a simple hello world that should demonstrate it’s capability can be as simple as this.
there are some 16 options in chalk when it comes to just setting a text color by calling one of the methods that is a color name, like so:
The full list of color options is as follows:
Sometimes I might want to have a certain background color for the terminal text when making my CLI tool, and just have the plain old white foreground color. This can be done with the background style methods.
The full list of options is the same as with foreground colors, I just need to to ad ‘bg’ to the front of the name and capitalize the name of the color.
there are some methods for logging text with certain text styles, such as bold, and underline. The support for these is mixed underline for example does not seem to work in windows 10 cmd.exe, and nothing with chalk works in cygwin64 terminal in windows as it has a color level of 0 there.
- underline (not working in windows 10 cmd.exe)
- visible (Text is emitted only if enabled)
Color support for will very across different terminals, for example cmd.exe in windows 10 seems to only support color level 2, which is only 16 colors. Some external terminals might not even support level 2, and ignore the text patterns that are used to switch text style, or even show them in the output.
To help with this there is the chalk.supportColor, and chalk level properties. These can be used to feature test for support and act accordingly.
As you may have gathered you can use the supportsColor property of chalk to find out if the terminal that you are using supports color in the first place or not.
You can use a chainable API to define a custom style, and set it to a variable, like so.
It is possible to set text color using a hex method. In the case of the terminal not supporting a certain color, it will be scaled down to whatever is close. That makes it a great choice allowing for more advanced control over terminal color, while still working okay if the terminal only supports 16 colors.
This method works the same way as hex, only I can pass some dec values in place of a hex string.
I have been following the development of chalk for some time now, it’s a fun little dependency for setting terminal color to help add emphasis for things like error messages. The go to solution for adding stylish text to CLI tools in node.js for sure.
Be sure to check out my many other posts on node.js and npm packages.