How to Style Command Line Output with Chalk Package in Node.js

Last updated on by Digamber

Being a developer, it’s our daily job to interact with the command-line; it is a crucial part of our lives, but sometimes we get bored because of its dull appearance.

What if i told you there is a facile way that can enhance our logs output look. Well, you heard it right; there is a package called Chalk, which can make our work accessible, and command-line logs will no longer look peculiar to us.

Adding Chalk to the node project is easy, and it can enormously enhance the look and feel of our logs. You can easily style the colorize the output; simultaneously, it also boosts the user experience.

The chalk library offers powerful and simple methods to enforce ANSI colors and styles to our terminal output. This package is trendy and gets more than 5 million downloaded a week, which is a lot.

Create Node Project

Before we begin, we need to set up a brand new Node application. We need to execute the couple of mentioned below commands through the command prompt or terminal window to create a new project.

However, you can skip this step if you are done with this step.

mkdir your-project-name

Head over to the project root:

cd your-project-name

Run the following command to scaffolding the package.json file. Evidently, It will smoothly install all the dependencies in the node_modules folder.

npm init

Install Chalk Package

Offcourse terminal string styling can be done right, but first, it needs to have a chalk package installed. You can do it using the following command:

npm install chalk

Load Chalk in Node

Now comes the second step. In this step, we have to load the chalk module, and it can be inoculated by defining the const; we name it to chalk and loaded the script using the required method.

const chalk = require('chalk');

Isn’t no-brainer?

Since we have defined the chalk const through the require method, now we can easily access all the methods and properties, which is imperative to style the terminal output.

Style Terminal Output with Chalk

Initially, i would like to show you a very basic chalk package example. In which i will give the simple color to a word that i will print on the terminal console.

console.log(chalk.red("Lost in cosmos!"));

This code needs to go to your server js file; in our case, we name it index.js, but you can give any generic name to it.

// index.js

const chalk = require('chalk');

console.log(chalk.red("Lost in cosmos!"));

It’s good to look at our code’s progress, So, run the following command from the terminal.

node index.js

Right after the execution of the above command, you will see the below output on your terminal window:

Style Command Line Output with Chalk Package in Node.js

Chalk Background Colors Examples

Let’s move to another step; now, we will see how to play with other chalk properties. I want to show you how to color the text background you can see on the terminal screen.

const chalk = require('chalk');


console.log(chalk.white.bgBlue.bold("The Big Bang Theory!"));

Here is the output:

Chalk Background Colors Examples

Some Advance Usage of Chalk

Chalk offers super easy to use composable API, which can easily be chained and even gives you the freedom to nest the styles that you always wanted to see on your terminal screen.

const chalk = require('chalk');
const log = console.log;
 
// Combine styled and normal strings
log(chalk.blue('Hello') + ' World' + chalk.red('!'));
 
// Compose multiple styles using the chainable API
log(chalk.blue.bgRed.bold('Hello world!'));
 
// Pass in multiple arguments
log(chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz'));
 
// Nest styles
log(chalk.red('Hello', chalk.underline.bgBlue('world') + '!'));
 
// Nest styles of the same type even (color, underline, background)
log(chalk.green(
    'I am a green line ' +
    chalk.blue.underline.bold('with a blue substring') +
    ' that becomes green again!'
));
 
// ES2015 template literal
log(`
CPU: ${chalk.red('90%')}
RAM: ${chalk.green('40%')}
DISK: ${chalk.yellow('70%')}
`);
 
// ES2015 tagged template literal
log(chalk`
CPU: {red ${cpu.totalPercent}%}
RAM: {green ${ram.used / ram.total * 100}%}
DISK: {rgb(255,131,0) ${disk.used / disk.total * 100}%}
`);
 
// Use RGB colors in terminal emulators that support it.
log(chalk.keyword('orange')('Yay for orange colored text!'));
log(chalk.rgb(123, 45, 67).underline('Underlined reddish color'));
log(chalk.hex('#DEADED').bold('Bold gray!'));

Execute the node index.js command and see the result on the terminal.

Some Advance Usage of Chalk

Summary

We have seen how to use the chalk package in simple Node.js application to style the terminal output. I hope you liked this article and share it with others as well.