Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Node.js · Module

node:console

The node:console module provides a global console and a configurable Console class for debugging and logging. Custom instances can direct regular output and errors to chosen writable streams.

How it works

The module exposes the Console class and a preconfigured global console. Instantiate Console with writable stdout and stderr streams to direct regular output separately from warnings and errors; the global instance targets process.stdout and process.stderr.

Syntax

new Console({ stdout, stderr })

Parameters

NameTypeDescription
stdoutstream.WritableWritable stream receiving log and informational output.
stderrstream.WritableWritable stream receiving warning and error output.

Returns

A Console instance when constructed with new Console(options).

Key points

  • Use the module for straightforward application diagnostics and debugging output.
  • Its public surface consists of the Console class and the global console instance.
  • The class supplies familiar methods including log, error, and warn.
  • Custom instances can target writable streams selected by the application.
  • The built-in console uses the process output streams by default.

Common mistakes

  • Importing the module is unnecessary when using the built-in global console.
  • Configure stderr deliberately when a logger must keep diagnostic output separate from ordinary logs.

Example: Create a file-backed logger

const { Console } = require('node:console');
const fs = require('node:fs');

const output = fs.createWriteStream('./app.log');
const logger = new Console({ stdout: output, stderr: output });

logger.log('started');
logger.error('something went wrong');
output.end();

Creates a Console instance that sends both normal and error messages to a writable file stream.

When to use it

Custom log routing

Send application logs to a file or other writable destination.

Split diagnostic streams

Keep warnings and errors separate from routine informational output.

Process debugging

Print quick diagnostics during development or command-line execution.

Interview questions

QWhat does node:console provide?

The module provides a Console class and a global console instance. The class writes through configurable streams, while the global instance is configured for process.stdout and process.stderr.

Follow-up: How would you route errors to a separate destination? Construct Console with the normal destination as stdout and a different writable stream as stderr.

QHow do you create a custom Console?

Use new Console({ stdout, stderr }), where stdout is the writable stream for log or informational output and stderr receives warnings and errors.

Follow-up: What happens if stderr is omitted? The documentation states that warning and error output is sent to stdout.

QHow is the global console configured?

The global console is equivalent to new Console({ stdout: process.stdout, stderr: process.stderr }). It is available directly without importing node:console.

Follow-up: Why use a custom instance instead? It lets the application choose writable destinations rather than the process defaults.

  • console.log()
  • console.error()
  • console.warn()
  • process.stdout
  • process.stderr

Source: Console. Checked 2026-10-15.