Do you know you can print your text in different styles and colors on the console? If not, then use the chalk module and make your terminal output stylish. By default, when you print a text on the console, it is printed in white color. However, using the chalk module, you can change the color and style of the text. For example, the chalk module can make your text colorful by changing its font and background color. In addition, you can also change the text's formatting styles, such as bold, italic, underline, etc.
This tutorial will teach you different ways of using the chalk module in Node.js.
Let's start by installing chalk in your project.
Before using the chalk module in your project, you need to install it using the npm install
command.
To install the chalk module, run the following code:
$npm install chalk OR $npm install chalk --save
This command will download all the necessary files of chalk package to the node_modules
folder and makes its entry in the dependencies
section of the package.json file.
If you have installed version 4.x, import the chalk package using the require()
method.
const chalk = require('chalk');
On the other hand, if you have installed version 5.x, use the import
keyword to import chalk in your code.
import chalk from 'chalk';
After importing the module, you use the chalk module like this:
chalk.style.[style...](text)
As you can see, different styles are chained, and the last one is called as a method. Style can be following:
Formatting Style | Description |
---|---|
bold | The text is printed in bold. |
italic | It prints the text in italic. |
underline | In this, a horizontal line is put below the text. |
inverse | It replaces background and foreground colors with each other. |
hidden | The text is printed but hidden from the user. |
Following are the colors that you can use with the chalk package:
For more formatting and color options, check out this link.
The background color is specified by writing bg
before the color name and capitalizing the first letter of that color. For example, for the red background color, write bgRed
.
Note: All the colors mentioned above can be used as background colors.
It's time to look at some examples that will improve your understanding of using chalk.
const chalk = require('chalk'); //Error message in red console.log(chalk.red.bold.bgWhite('Failed to connect!')); //Success message in green console.log(chalk.italic.bgGreen.black('Database connected!')); //Warning message in yellow console.log(chalk.yellow.underline('This function is deprecated.')); //Prints text in red color and blue background console.log(chalk.bgRed.blue.inverse('Inverse style'));
Note: The order of the styles does not matter. For example, chalk.red.bold.bgWhite('Failed to connect!') can be written as chalk.bold.bgWhite.red('Failed to connect!')
Output
Explanation of the code:
1. chalk.red.bold.bgBlack('Failed to connect!')
prints bold text in red color with black background.
2. chalk.bold.bgGreen.black('Database connected!')
prints italic text in black font color and green background color.
3. chalk.yellow.underline('This function is deprecated.')
prints underlined text in yellow color.
4. The inverse style replaces the background color with the foreground color and the foreground with the background.
chalk.bgRed.blue.inverse('Inverse style')
prints text in red font color with blue background.
You can also use the RGB color model with chalk. All you have to do is call rgb()
and pass the proportion of red, green and blue to it.
chalk.rgb(red, green, blue)
The range of these three colors is from 0 to 255. 0 means 0% of that color, and 255 means 100% of that color.
You can specify background color by prefixing the rgb()
with bg
and capitalizing the first letter of rgb()
.
chalk.bgRgb(red, green, blue)
For example, for turquoise background color, write bgRGB(48, 213, 200)
Let's rewrite the same JavaScript code using the RGB color model.
const chalk = require('chalk'); //Error message in red with white background. console.log(chalk.rgb(255, 0, 0).bold.bgRgb(255, 255, 255)('Failed to connect!')); //Success message in green background and white text. console.log(chalk.italic.bgRgb(0, 255, 0).rgb(0, 0, 0)('Database connected!')); //Warning message in yellow console.log(chalk.rgb(255, 255, 0).underline('This function is deprecated.')); //Prints text in red color and blue background console.log(chalk.bgRgb(255, 0, 0).rgb(0, 0, 255).inverse('Inverse style'));
The output of this code is similar to the previous one.
If you have a color in the hexadecimal format, then use hex()
and pass the hex code of the color to it.
chalk.hex('hexcode')
You can specify background color by prefixing hex()
with bg
and capitalize the first letter of hex()
. For example, for peach background color write bgHex('#FFE5B4')
.
const chalk = require('chalk'); console.log(chalk.hex('#FF0000').bold.bgHex('#FFFFFF')('Failed to connect!')); console.log(chalk.italic.bgHex('00FF00').hex('#000000')('Database connected!')); console.log(chalk.hex('#FFFF00').underline('This function is deprecated.')); console.log(chalk.bgHex('#FF0000').hex('#0000FF').inverse('Inverse style'));