Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

nodejs

How to give colored outputs using npm color modules

Faraz Karim

When developing a program, it is sometimes very convenient to color-code certain important messages to stand out from all other messages. You get to know the nature of the message just by looking at the color.

In Node.js, we have the color module that allows you to have different colored outputs in the terminal. The color module has several other functions and features which you can see from here, though in this article, we will be focusing on coloring our terminal outputs

Text colors

The colors module allows you to choose any one of the following colors for your output:

  • black
  • red
  • green
  • yellow
  • blue
  • magenta
  • cyan
  • white
  • grey

Installation of colors

You need the npm package manager to install the’ colors’ module, whose setup guidelines you can find here. With npm installed, you only need to run the following command in your terminal or command prompt:

npm install colors

Importing colors to your file

Now that you have installed colors, you need to import them into the file you plan to use. For this, you need to require it at the top of your file along with any other module you intend on importing:

require('colors')

Example of using colors

We are just importing the color module and assigning colors to each string we are printing on the console in the following program:

require('colors');

console.log('Blue'.blue);

console.log('Green'.green);

console.log('Red'.red);

console.log('Magenta'.magenta);

console.log('cyan'.cyan);

console.log('Grey'.grey);

The file above has the following output when it is run:

widget

Assigning colors to message types

Similar to what we have just done, we can also assign a color to a certain type of message category we have created. This means that all messages belonging to that message type would have the same color.

The three most basic types of message categories in any Node.js project are:

  • Warning message
  • Error message
  • Success message

Even these modules have further divisions like, for example; the error message type may have several types of errors like server error or connection failure, which would help to color code to know what sort of error you are dealing with.

The following examples show how you can color-code your error types:

index.js
error_colors.js
errors.js

const {SERVER_ERROR, 
CONNECTION_ERROR} = require('errors.js');

const colored_printer = require('./error_colors.js'); 

colored_printer( SERVER_ERROR, ' Server failure occured ');
colored_printer( CONNECTION_ERROR, ' Cannot connect to server ');

For the code above, we made the errors.js to store and export the different types of errors, the error_colors.js file to generate a colored output for each type of error. And finally, we made the index.js file, which imported and used both the error types and the colored_printer function.

This code produces the following output:

widget

RELATED TAGS

nodejs

CONTRIBUTOR

Faraz Karim
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring