How to Print Terminal Messages in Colors using Chalk NPM

We need to display messages on the terminal for server applications. Reading terminal messages while scanning for important messages is hard for the user. That is where NPM module Chalk comes into play. Chalk adds color to terminal messages making them easier to read.

What is Chalk NPM Package?

Chalk is a NPM package used to output terminal messages in colors. Terminal messages in color help users to read and understand at ease. Many npm packages – including npm itself – use Chalk. As of this writing, 75,000+ packages have Chalk as a dependency.
Chalk is only meant for server-side applications – it is not used for a front-end web applications. Chalk has no effect on Chrome Developer Console.

Installing chalk using NPM?

npm install chalk OR yarn add chalk

How to use Chalk?

Chalk is easy to use. Basic usage is like this.

const chalk = require('chalk'); console.log(chalk.blue('Hello world!'));
Code language: JavaScript (javascript)

You can always use template literal.

Use Chalk in Large Project

You can use Chalk in any server side Node project, such as ExpressJS, KoaJS etc. Chalk library is only meant for Terminal Messages.

Chalk can not be used for front end framework such as ReactJS, AngularJS or Svelte.

Color-coding individual terminal messages can be tedious. Instead of adding color to individual messages, we like to create a helper module. For Large Server Side Projects, it is useful to create a separate module – Let us name it as “log.js”. The log module can contains methods to output terminal messages based on log severity.

// Inside log.js file const chalk = require("chalk"); function error(...args) { console.log(chalk.bold.red(...args)); } function warn(...args) { console.log(chalk.keyword("orange")(...args)); } function log(...args) { console.log(chalk.grey(...args)); } function info(...args) { console.log(chalk.blue(...args)); } module.exports = { warn, error, info, log };
Code language: JavaScript (javascript)
// Now, in rest of your program // you can use log module - const log = require('./log'); log.warn( 'This will be a Orange Warning');
Code language: JavaScript (javascript)

How to print JSON on console?

I often find need to pretty print json. Printing json on console yields to so much insights for debugging. If you just pass json object to console.log – it doesn’t print the json – but just prints [Object]. That is not very useful for debugging. We need to print entire JSON Object.


There are many ways to Pretty Print JSON – simplest one is using in build ‘console.dir’ function. Following code snippet with print json object with any depth.

console.dir(jsonObject, { depth: null });
Code language: CSS (css)

If you need a prettier format for your json object, we can use npm module ‘json-colorizer’.

json-colorizer is a  a simple console syntax highlighter for JSON.

const colorize = require('json-colorizer'); console.log(colorize(jsonObject, { pretty: true }));
Code language: JavaScript (javascript)

PS:

Do you know you can also style Chrome Developer Console Messages? Your frontend applications could also use a pop of color
Open up your Chrome Browser Developer Tool and paste the following in Console. Send me the screenshot of what you see!


Try this –

console.log('%c Follow me on Twitter @meera_datey','color:green;border:3px solid blue;font-size: 25px');
Code language: JavaScript (javascript)
Google Developer Console Screen Shot

Leave a Reply

Your email address will not be published.