chalkjs command linting output styling

What is chalk in JS?

chalk 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 of the console or terminal output window.

It is a popular library used in Nodejs to apply syntax colors to create own themes to terminal windows.

Features:

  • Opensource
  • npm library provided
  • All colors are supported
  • Server-side support, not a browser-based library

You can also check other posts on npm command deprecate option is deprecated

install the npm library

Let’s first create a node application called chalkjsdemo.

First, create a chalkjsdemo directory

mkdir chalkjsdemo
cd chalkjsdemo

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

npm init -y

It creates a package.json file in the application root directory.

next, install the chalkjs library using the 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 the 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 the 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 a complete set of palette colors as seen below.

following are colors supported

foreground colorbackground color
redbgRed
greenbgGreen

Adding bold, underline, and Italic styles to the terminal console string

It is very easy to add bold to console strings.

In the same way, we can add underline, and 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"));

What is chalk NPM used for?

ChalkJS is used to apply styles, and themes to terminal output window text. Can be customized in nodejs applications to display styled messages during the webserver emit the console logs.