How to Style the Output Text of the Terminal Window in node.js


In this article, we show how to style the output text of the terminal window in node.js.

By styling, we mean that you can change the styles, such as whether the text is regular or bold, we can change the color of the text, or we can add a background color to the output text of the terminal.

We do this with the chalk npm package.

The chalk npm is a package that allows us to style the output text that appears in the terminal window in node.js.

This includes things such as the being able to change the color of the text, the boldness, and whether the text is underlined or not.

So the first thing that needs to be done is you need to install the chalk npm package.

This can be done with either


Once you have the chalk package installed, then it's time for us to build our program.

So below, we show various functions of the chalk package that can style text.

After running this code, we get the following output shown below.

Styling output text of the terminal window with the chalk npm package in node.js

Now let's go over the code above.

The first thing that we have to do is import the chalk package, in order to use it.

Next, we start our statements.

In the first console.log statement, we change the color of the text to red using the red function. Other colors that exist with the chalk module are black, green, yello, blue, magenta, cyan, and white.

You may see that many colors aren't present, such as orange, purple, violet, etc. Does this mean that you can't make the text these colors? No. We will show later how you can make the text be any custom color, including orange, purple, or anything else using the hex function and specifying the hex color code of the desired color.

In the second console.log statement, we make the text white and bold. The bold function, as its name suggests, makes the text bold (thicker).

The third console.log statements changes the background color of the text. Instead of the default black background, we change the background color to cyan.

In the next statement, we make the text a blue color with the background color being yellow.

In the next statement, we use the inverse function. What does this function do? It reverses the color of the background and text color. It switches the two. We specify here that the text to be a white color and then use the inverse function. This makes the text black and the background color white. This is because black is normally the default background. With the inverse function, the black becomes the text color and white becomes the background color.

The next statement makes the text color green and underlines it using the underline function.

The last statement uses the hex function, as previously discussed, which allows us to create text in any custom color by specifying the hex color code. In this example, we specify #FF8800, which represents orange.

So these are some of the functions and capabilities present in the chalk npm package which allows us to be able to style output text of the terminal window in node.js.

Related Resources

How to Find the Version of node.js Installed on Your Computer

HTML Comment Box is loading comments...