Home Node.js JavaScript chalk example

JavaScript chalk example

The Chalk module is a third-party module that is used for styling the format of text and allows us to create our own themes in a node.js project.

You can use it to customize the color of the command line output and you can create your own

Installation

npm install chalk

Examples

Basic text colors

Here are various colors

const chalk=require("chalk");

console.log(chalk.red('http://www.maxjavascript.com/'));
console.log(chalk.blue('http://www.maxjavascript.com/'));
console.log(chalk.green('http://www.maxjavascript.com/'));
console.log(chalk.cyan('http://www.maxjavascript.com/'));
console.log(chalk.white('http://www.maxjavascript.com/'));
console.log(chalk.magenta('http://www.maxjavascript.com/'));
console.log(chalk.yellow('http://www.maxjavascript.com/'));

You should see something like this

background colors

Here are some of the background colors that are available

const chalk=require("chalk");

console.log(chalk.bgRed('http://www.maxjavascript.com/'));
console.log(chalk.bgBlue('http://www.maxjavascript.com/'));
console.log(chalk.bgGreen('http://www.maxjavascript.com/'));
console.log(chalk.bgCyan('http://www.maxjavascript.com/'));
console.log(chalk.bgBlack('http://www.maxjavascript.com/'));
console.log(chalk.bgMagenta('http://www.maxjavascript.com/'));
console.log(chalk.bgYellow('http://www.maxjavascript.com/'));

You should see something like this when run

Creating your own theme

This shows how to create your own custom theme

const chalk=require("chalk");
  
// Creating theme
const error=chalk.red;
const warning=chalk.yellow;
const good=chalk.green;

// Printing theme text
console.log(error("Error : failure message"));
console.log(warning("Warning : be careful"));
console.log(good("Good to go"));

You should see something like this when run

chalk modifiers

There are several modifiers available such as bold, italic and underline.

This is an example

const chalk=require("chalk");

console.log(chalk.bold('http://www.maxjavascript.com/'));
console.log(chalk.italic('http://www.maxjavascript.com/'));
console.log(chalk.inverse('http://www.maxjavascript.com/'));
console.log(chalk.underline('http://www.maxjavascript.com/'));
console.log(chalk.strikethrough('http://www.maxjavascript.com/'));
console.log(chalk.dim('http://www.maxjavascript.com/'));

Links

https://github.com/chalk/chalk

You may also like