Skip to content
Tutorials Center
My learning

Topics

Node.js Reference ยท Globals and Console

The Console class

Reference for the Node.js Console class: build a console bound to your own writable streams, capture output, set group indentation and inspect options.

The global console is an instance of the Console class. The class itself is exported by node:console and is also available as console.Console. Creating your own instance lets you send log, warn, table, group and the rest of the console API to any writable stream instead of the process's standard output: a file, an in-memory buffer, a network socket or a test capture.

Every method you know from the global console exists on the new instance, with the same formatting. Only the destination changes, which makes Console a good fit for small loggers and for tests that need to assert on printed output.

NODE
import { Console } from 'node:console';

const logger = new Console({ stdout: process.stdout, stderr: process.stderr });
logger.log('hello');

Try it

Node.js Capturing console output in memory

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Node.js Separate streams, indentation and inspect options

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Syntax

NODE
new Console(stdout[, stderr][, ignoreErrors])
new Console(options)
Name Description
stdout A writable stream that receives log, info, debug, dir, table and group output. Required.
stderr A writable stream for error, warn and trace. Defaults to stdout.
ignoreErrors If true (the default), errors thrown while writing to a stream are ignored.
colorMode 'auto' (default), true or false. Controls ANSI colors in inspected values.
inspectOptions Options passed to util.inspect() for every value (depth, compact, breakLength, ...). Cannot be combined with colorMode.
groupIndentation Spaces added per group() level. Default 2.

Returns: a console object with the usual methods: log, info, debug, warn, error, dir, dirxml, table, trace, assert, count, countReset, group, groupEnd, time, timeEnd, timeLog.

Errors: a TypeError with code ERR_CONSOLE_WRITABLE_STREAM is thrown if stdout or stderr lacks a write() method.

Usage notes

  • Use new Console(...), not a call without new. The constructor can be called without new in current versions, but new is the documented form.
  • Group state is per instance. group() indentation, count() counters and time() labels are not shared between consoles, so a capture logger never interferes with the global console.
  • Streams are used as given. Console does not end or close them. If the stream is a file, close it yourself.
  • ignoreErrors: true hides write failures. Pass false while debugging a custom stream whose write may throw.
  • Colors are decided per stream. With colorMode: 'auto', a stream that is not a TTY gets no ANSI codes, so captured text stays clean.
  • No automatic timestamps or levels. Console only formats and writes. For levels, JSON lines or rotation, wrap it or use a logging library (see Logging).
  • Worker threads have their own global console that forwards output to the main thread; creating a Console on process.stdout there works the same way.