Node.js Console Complete Reference
The console module in Node.js provides a set of functions to output information to the terminal or command line, helping with debugging and logging. It is a built-in utility that is essential for monitoring and troubleshooting Node.js applications.
It is a global object that provides a simple debugging console similar to JavaScript to display different levels of the message. It is provided by web browsers.
Node.js Console Methods
The console module in Node.js provides a variety of methods that help in outputting information to the console:
Basic Logging:
- console.log(): Logs messages to the standard output.
- console.error(): Logs error messages to the standard error stream.
Formatting and Output:
- console.info(): Similar to console.log(), often used to log informational messages.
- console.warn(): Logs warnings to the standard output with a “warning” label.
- console.debug(): Logs debug information; often used in development.
Advanced Features:
- console.table(): Logs data in a tabular format, useful for arrays and objects.
- console.trace(): Prints a stack trace to the console, helping identify the call stack at a specific point in the code.
- console.time() and console.timeEnd(): Measure the time taken by code execution, useful for performance profiling.
Customizing Console Output:
- console.assert(): Logs a message if a specified condition is false, useful for debugging assertions.
- console.dir(): Displays an interactive list of the properties of a specified object.
Example:
// Node.js program to demonstrate the
// console.trace() method
// Accessing console module
const console = require('console');
// Calling console.trace() method
console.trace("stack teace sample");
console.trace("stack trace sample with args: %d", 39);
Output:
Trace: stack teace sample
at Object. (C:\nodejs\g\console\console_trace.js:4:9)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
Trace: stack trace sample with args: 39
at Object. (C:\nodejs\g\console\console_trace.js:5:9)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
The complete list of NodeJS Console are listed below:
Node.js Console | Description |
---|---|
Node.js console.assert() Method | It checks whether the value is true or not and print an error message, if provided and failed to assert the value. |
Node js console.clear() Method | The console.clear() will work differently across different operating systems and terminal types. |
Node.js console.count() Method | This is used to count label passed to it as a parameter, by maintaining an internal counter for that specific label. |
Node.js console.countReset() Method | This is used to reset the count for the specific label passed to it as a parameter. |
Node.js console.debug() Method | This is used to print messages to stdout in a newline. Similar to console.log() method. |
Node.js console.dir() Method | This is used to get the list of object properties of a specified object. |
Node.js console.error() Function | This is used to display an error messages on the console. It prints to stderr with newline. |
Node.js console.info() Method | This is used to print messages to stdout in a newline. It is similar to console.log() method. |
Node.js console.timeLog() Method | This is used to display the time for each execution. This function is proved to be effective when used in a loop. |
Node.js console.time() Method | It is used to starts a timer that is used to compute the time taken by a piece of code or function. |
Node.js console.table() Method | It is used to print the table constructed from it’s parameters into the console. |
Node.js new Console() Method | This is inbuilt application programming interface of the ‘console’ module which creates a new Console with single or multiple writable streams |
Node.js console.profile() Method | The console.profile() (Added in v8.0.0) method is an inbuilt application programming interface of the ‘console‘ module which doesn’t display anything unless used in the inspector. |
Node.js console.profileEnd() Method | The console.profileEnd() (Added in v8.0.0) method is an inbuilt application programming interface of the ‘console’ module which does not display anything unless used in the inspector. |
Node.js console.timeStamp() Method | This is an inbuilt application programming interface of the ‘console‘ module which does not display anything unless used in the inspector. |
Node.js console.timeEnd() Method | This method stops a timer that was previously started by using console.time() method and display the result using stdout. |
Node.js console.trace() Method | This is used to print stack trace messages to stderr in a newline. Similar to console.error() method. |
Node.js console.warn() Function | This is used to display the warning messages on the console. It prints to stderr with newline |
Summary
The console module in Node.js is an invaluable tool for logging, debugging, and inspecting applications. Its various methods make it easier to track application behavior, measure performance, and troubleshoot issues during development.