Tips on how to use NPM Chalk to colorize and format your console output | Digital Noch

Tips on how to use NPM Chalk to colorize and format your console output | Digital Noch

Think about your console.log having the ability to present a number of colours. Not solely would it not be colourful however it would even be useful. That is the place the NPM chalk module is available in very useful. With the NPM chalk module, you may sprinkle a number of colours to your boring black-and-white console output and format the textual content.

It’s going to add some life to your console by highlighting essential textual content and making the textual content extra readable. On this put up, you’ll discover ways to set up and use the NPM chalk module to colorize your console output. Let’s get began!

Desk of contents #

What’s NPM chalk? #

NPM chalk is a third-party Node.js module that’s used so as to add coloration and styling to textual content on the command-line output. It additionally permits creating your individual themes in your Node.js mission. As per the Chalk GitHub web page, it’s:

Terminal string styling finished proper

Chalk helps a number of textual content kinds with backgrounds supporting completely different colours and formatting. As an illustration, you may present textual content in purple coloration with a grey background that’s daring and underlined. The chalk module is simple to put in and use, relying in your terminal colours it might probably help a variety of colours.

It’s an actively maintained mission with none dependencies. Chalk additionally has an expressive API and it’s a performant library. Within the subsequent half, you’ll know in regards to the recognition of the NPM chalk module.

Reputation of Chalk #

As talked about within the Chalk modules’ GitHub web page, it’s utilized by ~86K packages, it additionally has 202 million downloads monthly on NPM as of Oct-2022. A fast comparability of Chalk, colours, and the cli-color module on NPM Traits reveals that Chalk could be very fashionable. It’s 10 occasions extra fashionable than the colours module and round 70 occasions extra fashionable than the cli-color module.

Use NPM chalk popularity trend compared to colors and cli-color module

Within the subsequent part, you’ll find out about a easy CLI software the place you’ll add the NPM chalk module to it.

Instance CLI software #

For a means to make use of the Chalk module in an present app, you’ll use this straightforward app which makes use of Node.js readline performance to take an enter and the way a fundamental output. You’ll be able to clone this software from GitHub utilizing git clone [email protected]:geshan/nodejs-readline.git after which set up the chalk module as proven within the following half.

Keep in mind you will want Node.js 17+ to run this software.

On the time of writing the most recent LTS model is Node.js 18 which is the advisable model to run this software.

To rapidly check this software you may execute node readline.js which can ask the next query you may reply:

node readline.js
What's 4x4 equals? 16
16 is right!

You will have to sort a solution and this system will inform you whether it is right or not. Until now you’ll not see any coloration or formatting. Within the subsequent part, we are going to add the chalk module to the applying so as to add that zing with colours and formatting.

Tips on how to set up chalk to an present app #

To put in the NPM chalk module to an present Node.js software it’s best to run:

npm set up –save chalk

On the time of writing, it would set up chalk model 5.1.0`. One essential factor to note right here is from model 5, chalk is ESM EcmaScript Module. This implies if you wish to use chalk with TypeScrpt or different construct instruments it would be best to use Chalk model 4. Learn extra on model 5 and the pure ESM module.

After getting put in chalk as an NPM module you need to use it within the script to get the reply for 4x4 as detailed within the consequent part.

Including chalk and formatting to CLI output #

So as to add the colours and formatting for the right or incorrect reply for 4x4, you may edit the readline-basic.mjs file as follows:

import * as readline from 'node:readline/guarantees';
import stdin as enter, stdout as output from 'node:course of';
import chalk from 'chalk';

const rl = readline.createInterface( enter, output );
const reply = await rl.query('What's 4x4 equals? ');

const right = chalk.inexperienced.bgGray.underline.daring;
const incorrect = chalk.purple.bgWhite.daring;
const isCorrect = reply.trim() === '16';
const output = isCorrect ? right(`$reply.trim() is right!`) : incorrect(`$reply.trim() is wrong. Attempt once more.`);

console.log(`Error: `, err);

course of.exit(1);

First, you may have imported the chalk NPM module on line 3. Then you may have added a brand new fashion known as right as the right fixed. This fixed will present inexperienced textual content on a grey background with daring and underlined textual content.

Equally, you may have additionally set an incorrect fashion which can present purple textual content on a white background with daring formatting. Subsequent, relying on whether or not the reply for 4x4 is right or not whether it is 16 or not the right or incorrect fashion is utilized then the colorized and formatted textual content is ready within the output fixed. Lastly, the output is printed on the display screen with console.log which can have the set colours and formatting.

While you run the script with the right and incorrect solutions it would yield the next output:

Colorful and formatted CLI output using NPM chalk module

There are different convinient NPM chalk choices you need to use, these are mentioned within the subsequent part.

Different NPM chalk choices #

There are different colours and formatting choices you need to use with NPM chalk. You can even set ranges for the chalk NPM module to point out the variety of colours from none (0) to 16 million true coloration help (3). All of the coloration choices and background coloration choices are additionally listed on the NPM chalk’s GitHub readme. Relying on the colour help of the CLI even Hex and RGB colours can be utilized within the output.

You can even go a number of parameters to chalk technique calls like:

import chalk from 'chalk';

console.log('This', 'is', 'a', 'lengthy', 'sentence', 'in', 'blue', 'utilizing', 'chalk'));

It’s going to consequence within the following output when run with node long-sentence.mjs:

Blue text with NPM chalk multiple parameters

You will discover all of the code examples from this put up within the pull request. Chalk is a really helpful NPM module you need to use for coloring and formatting the command line output.

Conclusion #

On this put up, you may have discovered in regards to the helpful NPM chalk module. Along with what’s chalk and its recognition you additionally gained information about the right way to set up and use it in a Node.js software. You additionally came upon in regards to the different choices accessible within the Chalk NPM module.

Preserve making the CLI outputs not solely colourful however nicely formatted too.

Pleased Coding with Chalk!

#NPM #Chalk #colorize #format #console #output

Related articles


Leave a reply

Please enter your comment!
Please enter your name here