chalkjs command lining output styling


chalkjs is a JavaScript library for styling the console output strings in a terminal. We can style using foreground and background colors, Underline,Bold and Rgb colors

This is a popular library used in Nodejs to apply syntax colors to

features

  • Opensource
  • npm library provided
  • All colours are supported
  • Server side support, not browser based library

install npm library

Let’s first create a node application called chalkjsdemo

First create a chalkjsdemo directory

mkdir chalkjsdemo
cd chalkjsdemo

next run npm init -y command to initialize node application in this folder

npm init -y

This will create a package.json file in the application root directory

next install chalkjs library using npm install command

npm i chalk

This adds an entry in package.json and installs dependency to node_modules of your application.

Create a main.js file ,add following lines of chalkjs code

main.js

const chalk = require('chalk');

console.log(chalk.red('Hello World'));
console.log(chalk.green('Welcome to ') + ' chalkjs' + chalk.yellow(' Application'));

And the console output in terminal is chalk example in nodejs

See how the console strings are colored and It looks like visual studio code syntax.

This way we can add syntax colors to console output strings in nodejs applications like angular,react and vuejs.

In this example, We added font colors and background color styles to strings

It has support for complete set of palette colors as seen below

following are colors supported

foreground color   backround color  
red bgRed
green bgGreen

Adding bold,underline and Italic styles to terminal console string

It is very easy to add bold to console strings.

The same way we can add underline,Italic to console strings.

console.log(
    chalk.underline.bold('Chalkjs terminal example styles')
);
console.log(
    chalk.green.underline.bold('Chalkjs terminal example styles')
);

console.log(
    chalk.green.bgRed.underline.bold('Chalkjs terminal styles')
);

and output

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.