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

₹25,000.00

SAP SD S4 HANA

SAP SD (Sales and Distribution) is a module in the SAP ERP (Enterprise Resource Planning) system that handles all aspects of sales and distribution processes. S4 HANA is the latest version of SAP’s ERP suite, built on the SAP HANA in-memory database platform. It provides real-time data processing capabilities, improved…
₹25,000.00

SAP HR HCM

SAP Human Capital Management (SAP HCM)  is an important module in SAP. It is also known as SAP Human Resource Management System (SAP HRMS) or SAP Human Resource (HR). SAP HR software allows you to automate record-keeping processes. It is an ideal framework for the HR department to take advantage…
₹25,000.00

Salesforce Administrator Training

I am text block. Click edit button to change this text. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
₹25,000.00

Salesforce Developer Training

Salesforce Developer Training Overview Salesforce Developer training advances your skills and knowledge in building custom applications on the Salesforce platform using the programming capabilities of Apex code and the Visualforce UI framework. It covers all the fundamentals of application development through real-time projects and utilizes cases to help you clear…
₹25,000.00

SAP EWM

SAP EWM stands for Extended Warehouse Management. It is a best-of-breed WMS Warehouse Management System product offered by SAP. It was first released in 2007 as a part of SAP SCM meaning Supply Chain Management suite, but in subsequent releases, it was offered as a stand-alone product. The latest version…
₹25,000.00

Oracle PL-SQL Training Program

Oracle PL-SQL is actually the number one database. The demand in market is growing equally with the value of the database. It has become necessary for the Oracle PL-SQL certification to get the right job. eLearning Solutions is one of the renowned institutes for Oracle PL-SQL in Pune. We believe…
₹25,000.00

Pega Training Courses in Pune- Get Certified Now

Course details for Pega Training in Pune Elearning solution is the best PEGA training institute in Pune. PEGA is one of the Business Process Management tool (BPM), its development is based on Java and OOP concepts. The PAGA technology is mainly used to improve business purposes and cost reduction. PEGA…
₹27,000.00

SAP PP (Production Planning) Training Institute

SAP PP Training Institute in Pune SAP PP training (Production Planning) is one of the largest functional modules in SAP. This module mainly deals with the production process like capacity planning, Master production scheduling, Material requirement planning shop floor, etc. The PP module of SAP takes care of the Master…
X
WhatsApp WhatsApp us
Call Now Button