Node.js console Module
console The module is one of the most commonly used built-in modules in Node.js. It provides a simple debugging console, similar to the JavaScript console object in the browser. This module is mainly used to print information to standard output (stdout) and standard error (stderr), and is an important tool for debugging and logging during development.
Basic Usage of the console Module
1. Print ordinary information
console.log() is the most commonly used method for printing ordinary information:
Example
console.
log
(
'Hello, Node.js!'
)
;
// Output: Hello, Node.js!
2. Print error messages
console.error() is used to print error messages, output to stderr by default:
Example
console.
error
(
'This is an error message'
)
;
// Output (red): This is an error message
3. Print warning messages
console.warn() is used to print warning messages:
Example
console.
warn
(
'This is a warning message'
)
;
// Output (yellow): This is a warning message
4. Print informational messages
console.info() The difference between console.log() is similar and is used to print informational messages:
Example
console.
info
(
'This is an informational message'
)
;
// Output: This is an informational message
Advanced Features of the console Module
1. Formatted output
The console module supports printf()-style formatted output similar to the C language:
Example
const
name
=
'Alice'
;
const
age
=
25
;
console.
log
(
'Name: %s, Age: %d'
,
name
,
age
)
;
// Output: Name: Alice, Age: 25
Supported format placeholders:
%s- string%d- number (integer or floating point)%i- integer%f- floating-point number%o- object%j- JSON
2. Timing functionality
console.time() and console.timeEnd() can be used to measure code execution time:
Example
console.
time
(
'array test'
)
;
const
arr
=
[
]
;
for
(
let i
=
0
;
i
<
1000000
;
i
++
)
{
arr.
push
(
i
)
;
}
console.
timeEnd
(
'array test'
)
;
// Output: array test: 12.345ms
3. Grouped output
console.group() and console.groupEnd() You can create collapsible message groups:
Example
console.
group
(
'User Details'
)
;
console.
log
(
'Name: Alice'
)
;
console.
log
(
'Age: 25'
)
;
console.
groupEnd
(
)
;
4. Table output
console.table() You can output arrays or objects in tabular form:
Example
const
users
=
[
{
name
:
'Alice'
,
age
:
25
}
,
{
name
:
'Bob'
,
age
:
30
}
]
;
console.
table
(
users
)
;
5. Assertion testing
console.assert() is used for simple assertion testing:
Example
const
x
=
5
;
console.
assert
(
x
===
10
,
'x should be 10'
)
;
// Output when the assertion is false: Assertion failed: x should be 10
Configuration Options of the console Module
1. Custom output streams
You can create a custom Console instance specifying different output streams:
Example
const
{
Console
}
=
require
(
'console'
)
;
const
fs
=
require
(
'fs'
)
;
const
output
=
fs.
createWriteStream
(
'./stdout.log'
)
;
const
errorOutput
=
fs.
createWriteStream
(
'./stderr.log'
)
;
const
logger
=
new
Console
(
output
,
errorOutput
)
;
logger.
log
(
'This will be written to stdout.log'
)
;
logger.
error
(
'This will be written to stderr.log'
)
;
2. Colors and styles
In terminals that support ANSI colors, console output can include colors:
Example
console.
log
(
'\x1b[36m%s\x1b[0m'
,
'Cyan colored text'
)
;
Or use third-party libraries such as chalk to simplify color control:
Example
const
chalk
=
require
(
'chalk'
)
;
console.
log
(
chalk.
blue
(
'Blue text'
)
)
;
Best Practices
- Production Logging: in production environments, it is recommended to use a dedicated logging library such as
winstonorbunyaninstead of console. - Avoid Overusing: use console only during development and debugging; unnecessary console statements should be removed from production code.
- Performance considerations: console.log is synchronous, so use it with caution in performance-sensitive scenarios.
- Error handling: use console.error to log error messages for easier distinction and filtering.
List of Methods and Properties
The following are the main methods and properties of the Node.js console module:
| Method/attribute | Description | Output stream | Version introduced |
|---|---|---|---|
| console.log([data][, ...args]) | Print to stdout, with a newline | stdout | 0.1.100 |
| console.error([data][, ...args]) | Print to stderr, with a newline | stderr | 0.1.100 |
| console.warn([data][, ...args]) | Alias for console.error | stderr | 0.1.100 |
| console.info([data][, ...args]) | Alias for console.log | stdout | 0.1.100 |
| console.debug([data][, ...args]) | Print debug information to stdout | stdout | 8.0.0 |
| console.dir(obj[, options]) | Use util.inspect() on an object and print the result | stdout | 0.1.100 |
| console.time([label]) | Start a timer | - | 0.1.100 |
| console.timeEnd([label]) | Stop the timer and print the result | stdout | 0.1.100 |
| console.timeLog([label][, ...data]) | Print the current timer value | stdout | 10.7.0 |
| console.trace([message][, ...args]) | Print a stack trace to stderr | stderr | 0.1.100 |
| console.assert(value[, message][, ...args]) | If value is falsy, the assertion fails and message is printed | stderr | 0.1.100 |
| console.clear() | Attempt to clear the console | - | 8.3.0 |
| console.count([label]) | Count the number of calls for label | stdout | 8.3.0 |
| console.countReset([label]) | Reset the counter | stdout | 8.3.0 |
| console.group([...label]) | Indent subsequent output | stdout | 8.5.0 |
| console.groupEnd() | End the current indentation group | stdout | 8.5.0 |
| console.table(tabularData[, properties]) | Print data in tabular form | stdout | 10.0.0 |
| console.dirxml(...data) | If supported, call console.log to print an XML/HTML tree | stdout | - |
Special Properties
| property | Description |
|---|---|
| console.stdout | A writable stream pointing to standard output |
| console.stderr | A writable stream pointing to standard error output |
Node.js's console module is a simple but powerful debugging tool, offering a variety of methods from basic logging to advanced features such as timing and tabular output. Mastering the console module can significantly improve development efficiency and the debugging experience.
Node.js Built-in Modules