Node.js Console Complete Reference
Last Updated :
08 Aug, 2024
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:
JavaScript
// 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:
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.