Setting the font color & style of the console in Node.js

Node.jsServer Side ProgrammingProgramming

You can use the chalk module to customize the statements printed on the console.By using it, one can change the text font-color to any color. One can also change the font-style to Bold, Italic or Underlined. Also, you can highlight the printed text.

Syntax

npm install chalk

Example 1

Create a file "color.js" and copy the following code snippet. After creating the file, use the command "node color.js" to run this code.

// Giving color to console statments

// Importing the chalk module
const chalk = require('chalk');

// Printing the text in blue color
console.log(chalk.blue('Welcome to Tutorials Point !'));

// Printing the text in red color
console.log(chalk.red('Error occured !!! '));

// If color is not present we can pass the
// RGB code to give color
console.log(chalk.rgb(100, 150, 70)
            ('Custom statement - Hi...'));

Output

C:\home\node>> node color.js

Example 2

// Giving color to console statments

// Importing the chalk module
const chalk = require('chalk');

// Setting background color to red
console.log(chalk.bgGreen('Welcome to Tutorials Point'));

// Setting background color to Bright Black
console.log(chalk.bgBlackBright('Hello User !!! '));

// Setting background color to Bright Red
console.log(chalk.bgRedBright('Hello... I am a custom color.'));

Output

C:\home\node>> node color.js

Example 3

// Giving color to console statments

// Importing the chalk module
const chalk = require('chalk');

// Setting the text style to Bold
console.log(chalk.bold('Welcome to Tutorials Point in bold'));

// Setting the text style to dim
console.log(chalk.dim('Welcome to Tutorials Point in Dim'));

// Setting the text style to Italic
console.log(chalk.italic('Welcome to Tutorials Point in Italic'));

// Setting the text style to Bold
console.log(chalk.underline('Welcome to Tutorials Point in Underline'));

Output

C:\home\node>> node color.js

raja
Updated on 18-Aug-2021 09:20:38

Advertisements