Open In App

How to use ‘Kleur’ Module in Node.js ?

kleur‘ module is an external NPM module that can manage color properties in JavaScript applications or projects. It allows developers or programmers to manipulate and use colors in their projects. By using kleur, we change the colors of the outputs in the console which make it looks good.

Installation: To use this module in our project, we’ve to install it first. Open the terminal, navigate to the same folder as your project, and use the commands to install this module. After this, a new JSON file, ‘package.json’ will be added to your project, which contains all the details of the module installed.



$ npm install kleur 

Folder structure:

 

 



Use the module: After installation, we have to import ‘kleur’ module using the require function and then used this to manipulate the colors of different texts in the output.

const kleur = require('kleur');

Run: After successfully importing and writing the code, we can run the code written in the file. Use the given command to run the javascript code. In this command index.js is the JavaScript file name.

$ node index.js

Here are some examples of how can we use “kleur” module:

Example 1:




// Require the 'kleur' module
const kleur = require('kleur');
  
// Create variables with colored strings
const red = kleur.green().bold('GeeksforGeeks');
const blueBg = kleur.bgBlue(
    'How to use Kleur module in node.js ?');
  
// Log the colored strings to the console
console.log(red);
console.log(blueBg);
console.log(boldYellow);
console.log(greenUnderline);

Output:

output 1

Example 2:




const { bold, green } = require('kleur');
  
console.log(bold().red('this is a bold red message'));
console.log(bold().italic(
    'this is a bold italicized message'));
console.log(bold().yellow().bgRed().italic(
    'this is a bold yellow italicized message'));
console.log(green().bold().underline(
    'this is a bold green underlined message'));

Output:

output 2


Article Tags :