Node.js console module explanation with examples

How to use global console instance and console class in Node.js:

Node.js provides a module called console to write debugging data on the console similar to the Javascript console provides by the web browser. This module exports two components :

  1. One ‘Console’ class to write logs to a Node.js stream.

  2. One Global console instance to write to ‘process.stdout’ and ‘process.stderr’.

We can use the global console instance without using require(‘console’) at the beginning.

In this tutorial, we will learn how to use the global console instance and ‘Console’ class to write debug, warn and error logs.

Note that console module provides a lot of different methods to log errors, but we are using only debug, warn and error logs in this article.

Using the global console instance:

We can use the global console instance without using the require statement. For example:

console.log("hello");

const w = "World";

console.log(`Hello ${w}`);

console.warn("Error detected !!")

console.error(new Error("Oops !! Something happened"));

If you run this program, this will print the logs and errors like below:

Nodejs console module example

The output is sent to process.stdout and process.stderr.

Using the Console class:

To use the Console class, we need to create one Console object. The easiest way to create a Console object is by using the new operator :

new Console(options)

options is an object with the following key-value pairs :

stdout: Stream to write the logs. stderr(optional): Stream to write the error and warning logs. If this is not provided, stdout is used to write the error and warn logs. ignoreErrors(optional) : Boolean value, default is true. Indicates to ignore errors while writing to the underlying stream. colorMode(optional) : sets the color support for this instance. You can’t use this value if inspectOptions.colors is set. Default is ‘auto’ and you can set it to ‘true’.

Following example uses the ’Console’ class instance :

const fs = require('fs')
const { Console } = require("console");

const outFile = fs.createWriteStream('./stdout.log');
const errFile = fs.createWriteStream('./stderr.log');

const console = new Console({ stdout: outFile, stderr: errFile})


console.log("hello");

const w = "World";

console.log(`Hello ${w}`);

console.warn("Error detected !!")

console.error(new Error("Oops !! Something happened"));

If you execute this program, it will not show any logs on the console. Instead, it will write all of them in the stdout.log and stderr.log files in the same folder.

stdout.log will hold only the normal logs:

hello
Hello World

stderr.log will hold the errors and warning logs:

Error detected !!
Error: Oops !! Something happened
    at Object. (/Volumes/Main/example/example.js:18:15)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:236:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:560:3)

This process is useful for a live application. We can dump the logs in different files and later we can analyze them.