• Node.js Video Tutorials

Node.js - Console Module



NodeJS Console is a global object that enables a convenient debugging console to display different message levels. This debugging console is identical to JavaScript console mechanism provided by web-browsers. The console module has two specific components, which are described as below −

  • Console class − Methods like console.log(), console.warn(), and console.error are present in the console class. Any Node.js stream can be written using these methods.

  • Global console − The global console instance is configured to write process.stdout and process.stderr(this property returns a stream connected to stdout). It is possible to operate  without using the require("node:console") function.

List of Methods

Following are the list of methods available in the console class −

Sr.No Module & Description
1

assert()

Used to verify the invariants.

2

clear()

Used to clear the stdout (standard output).

3

count()

Used to get the count of the number of times the function has been called with a specific input value.

4

countReset()

Used to reset the count for the specific input value which is passed as a label.

5

debug()

Used to print information to stdout in a new line

6

dir()

Used to get the properties of a particular object.

7

dirxml()

Used to call the console.log() method at the time of execution by passing the arguments received.

8

error()

Used to display the error messages on the console.

9

group()

Used to get the passed information in the method as a grouped format.

10

groupCollapsed()

Used to collapse the previously created group by console.group().

11

groupEnd()

Used to end the group; which was created with console.group() and console.groupCollapsed() methods.

12

info()

Used to print information to stdout in a new line.

13

log()

Used to print information to stdout in a new line.

14

table()

Used to create a table in the console.

15

time()

Used to start a timer that we can use to track the time taken by an operation or any function.

16

timeEnd()

Used to stop the timer which was previously started by console.time() method.

17

timeLog()

Used to print the elapsed time for a timer that was previously started by calling console.time().

18

trace()

Used to print the stack 'Trace' to the current position of the code followed by the message and substitution values on stderr in a new line.

19

warn()

Used to print the warning messages to the console.

20

new Console()

creates a new Console with one or two instances of writable streams, namely stdout and stderr.

Inspector only methods

The following methods doesn't display anything unless used in the browser inspector.

Sr.No Module & Description
1

profile()

Used to start a JavaScript CPU profile.

2

profileEnd()

Used to stop the JavaScript CPU profiling session of the profile, which has been called previously.

3

timeStamp()

Used to add an event with or without the label to the timeline panel of the inspector tab.

Advertisements