Console
Stability: 2 - Stable
node:console module provides a simple debugging console that is similar to the JavaScript console mechanism provided by web browsers.
The module exports two specific components:
- A
Consoleclass with methods such asconsole.log(),console.error(), andconsole.warn()that can be used to write to any Node.js stream. - A global
consoleinstance configured to write toprocess.stdoutandprocess.stderr. The globalconsolecan be used without callingrequire('node:console').
Example using the global console
Example using the Console class
Class: Console
TheConsole class can be used to create a simple logger with configurable output streams.
new Console(options)
Configuration options for the Console instance
console.assert(value[, …message])
The value tested for being truthy
All arguments besides value are used as error message
value is falsy or omitted. It only writes a message and does not otherwise affect execution. The output always starts with “Assertion failed”.
console.clear()
Whenstdout is a TTY, calling console.clear() will attempt to clear the TTY. When stdout is not a TTY, this method does nothing.
console.count([label])
The display label for the counter
label and outputs to stdout the number of times console.count() has been called with the given label.
console.countReset([label])
The display label for the counter
label.
console.debug(data[, …args])
Theconsole.debug() function is an alias for console.log().
console.dir(obj[, options])
Object to inspect
util.inspect() on obj and prints the resulting string to stdout. This function bypasses any custom inspect() function defined on obj.
console.error([data][, …args])
Primary message
Substitution values
stderr with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3).
console.group([…label])
Labels to print before increasing indentation
groupIndentation length.
If one or more labels are provided, those are printed first without the additional indentation.
console.groupCollapsed()
An alias forconsole.group().
console.groupEnd()
Decreases indentation of subsequent lines by spaces forgroupIndentation length.
console.info([data][, …args])
Theconsole.info() function is an alias for console.log().
console.log([data][, …args])
Primary message
Substitution values
stdout with newline. Multiple arguments can be passed, with the first used as the primary message and all additional used as substitution values similar to printf(3).
console.table(tabularData[, properties])
Data to display as a table
Alternate properties for constructing the table
tabularData and rows of tabularData and log it. Falls back to just logging the argument if it can’t be parsed as tabular.
console.time([label])
Label for the timer
label. Use the same label when calling console.timeEnd() to stop the timer and output the elapsed time in suitable time units to stdout.
console.timeEnd([label])
Label for the timer
console.time() and prints the result to stdout.
console.timeLog([label][, …data])
Label for the timer
Additional data to log
console.time(), prints the elapsed time and other data arguments to stdout.
console.trace([message][, …args])
Message to display
Substitution values
stderr the string ‘Trace: ’, followed by the util.format() formatted message and stack trace to the current position in the code.
console.warn([data][, …args])
Theconsole.warn() function is an alias for console.error().
Inspector Only Methods
The following methods are exposed by the V8 engine in the general API but do not display anything unless used in conjunction with the inspector (--inspect flag).
console.profile([label])
Profile label
console.profileEnd() is called. The profile is then added to the Profile panel of the inspector.
console.profileEnd([label])
Profile label
console.timeStamp([label])
Event label