×

Node.js Console

Node.js Console: Like the JavaScript console mechanism, a console module is designed for Node.js that provides a debugging console for writing or printing messages.

Usually, in JavaScript, a console mechanism is used to write or print messages in console to understand specific functionality. Like this, we have a console module designed for Node.js to trace the functionality of specific activity by printing messages at discrete points.

There are two specific components exported by the console module, which are as follows:

  1. Console class: A console class contains methods that can be used to write any Node.js stream. These methods include console.log(), console.error(), and console.warn().
  2. Global console: A global console object is designed to write stdout and stderr. It can be utilized without calling require('console'). The writes of the Global console object's methods can vary from synchronous to asynchronous liable on what the stream is connected to and whether the operating system used is Windows or POSIX.

Global Console Instance in Node.js

Let us understand the concept behind the Node.js Console Object with an example. Here, we have made a simple logging program called 'global_console.js' to understand the global console's functioning.

File: global_console.js

console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('System has detected an Error'));
// Prints: [Error: System has detected an Error], to stderr
const name = 'Suhail Khan';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Suhail Khan! Danger!, to stder

As we can observe from the above code, we've written messages to node.js stream by utilizing global console object methods, which include console.log(), console.error(), and console.warn(). Here, we are retrieving the global console object deprived of being imported using the require() directive.

To see the output of the above program, we'll be using a Node.js Interpreter and navigate it to the folder containing our 'global_console.js' file and type –

node file_name.js 

(e.g., node global_console.js)

Node.js Console


As we can observe that we can write the required messages to node.js stream by utilizing the global console instance to trace down the functionality of any required activity.

Console Class in Node.js

The Console class of Node.js can be utilized to design a simple logging program with configurable output streams and can be retrieved by utilizing either 'require('console').Console' or 'console.Console'.

Let us understand this concept of using Console class with an example given below. Here, we've created a .js file called 'console_class.js' and type the following program.

File: console_class.js

const fs = require('fs')
const out = fs.createWriteStream('./stdout.log');
const err = fs.createWriteStream('./stderr.log');
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('An Unexpected Error occurs'));
// Prints: [Error: An Unexpected Error occurs], to err
const name = 'Suhail Khan';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Suhail Khan! Danger!, to err

In above code, we've created a Console class using 'console.Console'. Now, we will run the 'console_class.js' file in node.js interpreter to see the output.

Node.js Console

The following node.js example will make the log files (stdout and stderr) in the folder where the 'console_class.js' file is present with the messages entered demonstrated as follows:

Node.js Console

Console Methods in Node.js

Apart from console.log(), console.error(), and console.warn(); there is a list of methods available in Node.js with the global console instance.

S.No.MethodDescription
1console.log([data][, …])It is used to print the messages on the console. It prints stdout with a newline.
2console.info([data][, …])It is also used to write the messages on the console, and the purpose is an alias for console.log().
3console.error([data][, …])It is used to display the error messages on the console and prints to stderr with a newline.
4console.warn([data][, …])It is used to display the warning messages on the console and prints to stderr with a newline.
5console.dir(obj[, …])It uses util.inspect() method on obj and prints the resulting string to stdout.
6console.time(label)It begins the time to compute the duration of an operation.
7console.timeEnd(label)It ends the time that is formally begun by console.time().
8console.trace(message[, …])It is utilized to stack trace to the present location in the code and prints strerr the string 'Trace: ', followed by the formatted message.
9console.assert(value[, …])It is used to write a message if a value is false or omitted.
10console.clear()It is used to clear the history of the console.

Related Topics

Node.js NPM

Node.js – Node Package Manager NPM is an abbreviation for Node Package Manager. It is the default package manager developed by Isaac Z. Schlueter using JavaScript Programming Language, designed for JavaScript Runtime...

7 minutes read.

Difference Between Nodejs Angular and React

A Right JavaScript Framework Within a business, it is necessary to pick the right technology. As much as this applies to the web application and software development, it also applies in...

5 minutes read.

Node.js Console

Node.js Console: Like the JavaScript console mechanism, a console module is designed for Node.js that provides a debugging console for writing or printing messages. Usually, in JavaScript, a console mechanism is...

4 minutes read.

Node.js Discovering Timers

Node.js Discovering Timers: Timers are multi-lingual, and much functional in countless use cases. The Timers are mostly used in many popular programming languages, including Node.js. However, it's pretty simple to...

4 minutes read.

Node.js Buffers

An Introduction to Buffers A Buffer is an area in memory (typically RAM) that is used to store binary data. Binary is a collection or a set of 0 and 1....

4 minutes read.

Node.js REPL

Node.js REPL: REPL represents the Read Eval Print Loop. REPL is modest; much more, an interactive software development environment similar to the command line shells used in various Operating Systems....

4 minutes read.

Node.js File System

Introduction to File System in Node.js FS, abbreviated as File System, is a Node.js Build-in Module, which provides an Application Programming Interface (API) to interact with the File System, and to...

15 minutes read.

Difference Between Node.js and Django

Node.js vs Django In Web Application Development, we must have a robust technology to provide strength to our Web application. Node.js and Django both are pretty commanding technologies that help us...

6 minutes read.

Node.js Events Module

Introduction to the Node.js events Module As we’ve already discussed, Node.js is a JavaScript runtime with an event-driven architecture designed to perform Asynchronous tasks. In order to accomplish the event-driven architecture,...

4 minutes read.

Node.js Tutorial

Introduction to Node.js: Node.js is a non-proprietary and platform-independent runtime environment for implementing JavaScript code outside of a browser. However, NodeJS isn't a framework and not a programming language. We...

4 minutes read.

Node.js Callback Concept

Understanding the Concept of Callback A Callback is an asynchronous equivalent for a function that is called at the completion of a task. The Callback helps to avoid any I/O blocking and allows other code to execute...

2 minutes read.

Node.js Event Loop

An Introduction to Event Loop As we have already discussed, Node.js is an Asynchronous platform with a non-blocking I/O. Node.js has awesome features that made it this thriving, and the Event...

5 minutes read.

Node.js Core Debugger

Node.js – Core Debugger: It’s always a challenge when it comes to debug an application. The Asynchronous workflows of Node.js include an additional layer of intricacy to this difficult process....

3 minutes read.

Node.js Streams

An Introduction to the Streams Streams are one of the elemental concepts that are introduced to power the applications based on Node.js. These are the method used to handle the data...

6 minutes read.

Node.js Path Module

Understanding the path Module in Node.js The path module is a built-in module of Node.js that provides various methods to interact with the files and directory path names on the device’s...

6 minutes read.

Node.js HTTP Module

An Introduction to HTTP Module The http module is a core module in Node.js that uses the Hypertext Transfer Protocol (HTTP) to transfer data. With the help of the http module,...

7 minutes read.

Node.js Modules

Node.js Modules: At the point when we'll begin composing Node.js applications, we would place all our code into one big node.js file, regardless of how enormous or complex our application...

4 minutes read.

How to Install Node.js on Windows?

Environment Setup for Node.js Since Node.js is a mandatory section of the program that runs JavaScript, similar to all other required section of programs that run a programming language, it is...

4 minutes read.

Node.js Event Loop Phases

Node.js – Phases of Event Loop Once we have understood the Node.js event loop concept and the basic introduction to its phases, let's discuss the functions of the Check handler phase...

4 minutes read.

Difference Between Node.js and PHP

Node.js vs PHP Node.js and PHP both are the powerful back-ends to create versatile websites. Even when these two technologies fall under the same category, their features are pretty diverse. There's...

6 minutes read.