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.
import { Console } from 'node:console'; const logger = new Console({ stdout: process.stdout, stderr: process.stderr }); logger.log('hello');
Try it
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.
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
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 withoutnew. The constructor can be called withoutnewin current versions, butnewis the documented form. - Group state is per instance.
group()indentation,count()counters andtime()labels are not shared between consoles, so a capture logger never interferes with the global console. - Streams are used as given.
Consoledoes not end or close them. If the stream is a file, close it yourself. ignoreErrors: truehides write failures. Passfalsewhile debugging a custom stream whosewritemay 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.
Consoleonly 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
Consoleonprocess.stdoutthere works the same way.