Node.js Console Module

A text-based user interface called a Node.js console enables code execution or interaction with the computer’s operating system. It is frequently used for message logging, debugging, and troubleshooting.

Similar to the JavaScript console mechanism seen in web browsers, Node.js has an integrated Console module that offers the ability to print messages to IO streams.

Key Features of the Console Module

The Console module has two major components:

1. Console Class

The Console class provides methods such as:

  • console.log() – Displays general logs.
  • console.error() – Outputs error messages.
  • console.warn() – Displays warnings.

2. Global Console Object

The global console object records messages, errors, and warnings to the standard output stream using a specified object. Using require(‘console’) to import the Console module explicitly is not necessary.

Example Usage

The following code demonstrates basic usage of the global console object:

// Display a welcome message
console.log('Welcome to thefullstack!');

// Print a general message
console.log('Hello, world!');

// Print an error message
console.error(new Error('Oops... something went wrong'));

// Print a warning message
const name = 'Node.js Tutorial';
console.warn(`Warning ${name}! Warning!`);

Output

Welcome to thefullstack!
Hello, world!
Error: Oops... something went wrong
at Object.<anonymous> (D:\nodejs\app\main.js:11:15)
at Module._compile (node:internal/modules/cjs/loader:1241:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1295:10)
at Module.load (node:internal/modules/cjs/loader:1091:32)
at Module._load (node:internal/modules/cjs/loader:938:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12)
at node:internal/main/run_main_module:23:47
Warning Node.js Tutorial! Warning!

Customizing Console Output

Redirecting failures and logs to various output streams allows you to personalize the console object. Logs and errors are written to distinct files using the following program:

const fs = require('fs');

const out = fs.createWriteStream('./stdout.log');
const err = fs.createWriteStream('./stderr.log');

const logger = new console.Console(out, err);

// Logging messages to files
logger.log('Welcome to thefullstack!');
logger.log('Hello, world!');
logger.error(new Error('Oops... something went wrong'));

const name = 'Node.js Tutorial';
logger.warn(`Warning ${name}! Warning!`);

This code creates two files, stdout.log and stderr.log, in the current directory, storing logs and error messages separately.

Console Methods

The following methods are available in the console object:

MethodDescription
console.log([data][, ...])Prints to stdout with a newline. Supports multiple arguments similar to printf().
console.info([data][, ...])Alias for console.log().
console.error([data][, ...])Prints errors to stderr with a newline.
console.warn([data][, ...])Prints warnings to stderr with a newline.
console.dir(obj[, options])Uses util.inspect() on an object and prints the result to stdout.
console.time(label)Starts a timer with a given label.
console.timeEnd(label)Ends the timer and prints the elapsed time.
console.trace(message[, ...])Prints a stack trace with an optional message.
console.assert(value[, message][, ...])Similar to assert.ok(), but the error message is formatted using util.format().

This module is essential for debugging, logging, and performance tracking in Node.js applications.

It might be helpful:

Getting Started with Node.js: Your First Application

Best Practices for API Design in Full-Stack Development

WhatsApp WhatsApp us
Call Now Button