This is a popular library used in Nodejs to apply syntax colors to
- 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
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
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|
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') );