{ "type": "module", "source": "doc/api/console.md", "modules": [ { "textRaw": "Console", "name": "console", "introduced_in": "v0.10.13", "stability": 2, "stabilityText": "Stable", "desc": "

The console module provides a simple debugging console that is similar to the\nJavaScript console mechanism provided by web browsers.

\n

The module exports two specific components:

\n\n

Warning: The global console object's methods are neither consistently\nsynchronous like the browser APIs they resemble, nor are they consistently\nasynchronous like all other Node.js streams. See the note on process I/O for\nmore information.

\n

Example using the global console:

\n
console.log('hello world');\n// Prints: hello world, to stdout\nconsole.log('hello %s', 'world');\n// Prints: hello world, to stdout\nconsole.error(new Error('Whoops, something bad happened'));\n// Prints: [Error: Whoops, something bad happened], to stderr\n\nconst name = 'Will Robinson';\nconsole.warn(`Danger ${name}! Danger!`);\n// Prints: Danger Will Robinson! Danger!, to stderr\n
\n

Example using the Console class:

\n
const out = getStreamSomehow();\nconst err = getStreamSomehow();\nconst myConsole = new console.Console(out, err);\n\nmyConsole.log('hello world');\n// Prints: hello world, to out\nmyConsole.log('hello %s', 'world');\n// Prints: hello world, to out\nmyConsole.error(new Error('Whoops, something bad happened'));\n// Prints: [Error: Whoops, something bad happened], to err\n\nconst name = 'Will Robinson';\nmyConsole.warn(`Danger ${name}! Danger!`);\n// Prints: Danger Will Robinson! Danger!, to err\n
", "classes": [ { "textRaw": "Class: Console", "type": "class", "name": "Console", "meta": { "changes": [ { "version": "v8.0.0", "pr-url": "https://github.com/nodejs/node/pull/9744", "description": "Errors that occur while writing to the underlying streams will now be ignored by default." } ] }, "desc": "

The Console class can be used to create a simple logger with configurable\noutput streams and can be accessed using either require('console').Console\nor console.Console (or their destructured counterparts):

\n
const { Console } = require('console');\n
\n
const { Console } = console;\n
", "methods": [ { "textRaw": "console.assert(value[, ...message])", "type": "method", "name": "assert", "meta": { "added": [ "v0.1.101" ], "changes": [ { "version": "v10.0.0", "pr-url": "https://github.com/nodejs/node/pull/17706", "description": "The implementation is now spec compliant and does not throw anymore." } ] }, "signatures": [ { "params": [ { "textRaw": "`value` {any} The value tested for being truthy.", "name": "value", "type": "any", "desc": "The value tested for being truthy." }, { "textRaw": "`...message` {any} All arguments besides `value` are used as error message.", "name": "...message", "type": "any", "desc": "All arguments besides `value` are used as error message.", "optional": true } ] } ], "desc": "

A simple assertion test that verifies whether value is truthy. If it is not,\nAssertion failed is logged. If provided, the error message is formatted\nusing util.format() by passing along all message arguments. The output is\nused as the error message.

\n
console.assert(true, 'does nothing');\n// OK\nconsole.assert(false, 'Whoops %s work', 'didn\\'t');\n// Assertion failed: Whoops didn't work\n
\n

Calling console.assert() with a falsy assertion will only cause the message\nto be printed to the console without interrupting execution of subsequent code.

" }, { "textRaw": "console.clear()", "type": "method", "name": "clear", "meta": { "added": [ "v8.3.0" ], "changes": [] }, "signatures": [ { "params": [] } ], "desc": "

When stdout is a TTY, calling console.clear() will attempt to clear the\nTTY. When stdout is not a TTY, this method does nothing.

\n

The specific operation of console.clear() can vary across operating systems\nand terminal types. For most Linux operating systems, console.clear()\noperates similarly to the clear shell command. On Windows, console.clear()\nwill clear only the output in the current terminal viewport for the Node.js\nbinary.

" }, { "textRaw": "console.count([label])", "type": "method", "name": "count", "meta": { "added": [ "v8.3.0" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`label` {string} The display label for the counter. **Default:** `'default'`.", "name": "label", "type": "string", "default": "`'default'`", "desc": "The display label for the counter.", "optional": true } ] } ], "desc": "

Maintains an internal counter specific to label and outputs to stdout the\nnumber of times console.count() has been called with the given label.

\n\n
> console.count()\ndefault: 1\nundefined\n> console.count('default')\ndefault: 2\nundefined\n> console.count('abc')\nabc: 1\nundefined\n> console.count('xyz')\nxyz: 1\nundefined\n> console.count('abc')\nabc: 2\nundefined\n> console.count()\ndefault: 3\nundefined\n>\n
" }, { "textRaw": "console.countReset([label])", "type": "method", "name": "countReset", "meta": { "added": [ "v8.3.0" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`label` {string} The display label for the counter. **Default:** `'default'`.", "name": "label", "type": "string", "default": "`'default'`", "desc": "The display label for the counter.", "optional": true } ] } ], "desc": "

Resets the internal counter specific to label.

\n\n
> console.count('abc');\nabc: 1\nundefined\n> console.countReset('abc');\nundefined\n> console.count('abc');\nabc: 1\nundefined\n>\n
" }, { "textRaw": "console.debug(data[, ...args])", "type": "method", "name": "debug", "meta": { "added": [ "v8.0.0" ], "changes": [ { "version": "v9.3.0", "pr-url": "https://github.com/nodejs/node/pull/17033", "description": "`console.debug` is now an alias for `console.log`." } ] }, "signatures": [ { "params": [ { "textRaw": "`data` {any}", "name": "data", "type": "any" }, { "textRaw": "`...args` {any}", "name": "...args", "type": "any", "optional": true } ] } ], "desc": "

The console.debug() function is an alias for console.log().

" }, { "textRaw": "console.dir(obj[, options])", "type": "method", "name": "dir", "meta": { "added": [ "v0.1.101" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`obj` {any}", "name": "obj", "type": "any" }, { "textRaw": "`options` {Object}", "name": "options", "type": "Object", "options": [ { "textRaw": "`showHidden` {boolean} If `true` then the object's non-enumerable and symbol properties will be shown too. **Default:** `false`.", "name": "showHidden", "type": "boolean", "default": "`false`", "desc": "If `true` then the object's non-enumerable and symbol properties will be shown too." }, { "textRaw": "`depth` {number} Tells [`util.inspect()`][] how many times to recurse while formatting the object. This is useful for inspecting large complicated objects. To make it recurse indefinitely, pass `null`. **Default:** `2`.", "name": "depth", "type": "number", "default": "`2`", "desc": "Tells [`util.inspect()`][] how many times to recurse while formatting the object. This is useful for inspecting large complicated objects. To make it recurse indefinitely, pass `null`." }, { "textRaw": "`colors` {boolean} If `true`, then the output will be styled with ANSI color codes. Colors are customizable; see [customizing `util.inspect()` colors][]. **Default:** `false`.", "name": "colors", "type": "boolean", "default": "`false`", "desc": "If `true`, then the output will be styled with ANSI color codes. Colors are customizable; see [customizing `util.inspect()` colors][]." } ], "optional": true } ] } ], "desc": "

Uses util.inspect() on obj and prints the resulting string to stdout.\nThis function bypasses any custom inspect() function defined on obj.

" }, { "textRaw": "console.dirxml(...data)", "type": "method", "name": "dirxml", "meta": { "added": [ "v8.0.0" ], "changes": [ { "version": "v9.3.0", "pr-url": "https://github.com/nodejs/node/pull/17152", "description": "`console.dirxml` now calls `console.log` for its arguments." } ] }, "signatures": [ { "params": [ { "textRaw": "`...data` {any}", "name": "...data", "type": "any" } ] } ], "desc": "

This method calls console.log() passing it the arguments received.\nPlease note that this method does not produce any XML formatting.

" }, { "textRaw": "console.error([data][, ...args])", "type": "method", "name": "error", "meta": { "added": [ "v0.1.100" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`data` {any}", "name": "data", "type": "any", "optional": true }, { "textRaw": "`...args` {any}", "name": "...args", "type": "any", "optional": true } ] } ], "desc": "

Prints to stderr with newline. Multiple arguments can be passed, with the\nfirst used as the primary message and all additional used as substitution\nvalues similar to printf(3) (the arguments are all passed to\nutil.format()).

\n
const code = 5;\nconsole.error('error #%d', code);\n// Prints: error #5, to stderr\nconsole.error('error', code);\n// Prints: error 5, to stderr\n
\n

If formatting elements (e.g. %d) are not found in the first string then\nutil.inspect() is called on each argument and the resulting string\nvalues are concatenated. See util.format() for more information.

" }, { "textRaw": "console.group([...label])", "type": "method", "name": "group", "meta": { "added": [ "v8.5.0" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`...label` {any}", "name": "...label", "type": "any", "optional": true } ] } ], "desc": "

Increases indentation of subsequent lines by two spaces.

\n

If one or more labels are provided, those are printed first without the\nadditional indentation.

" }, { "textRaw": "console.groupCollapsed()", "type": "method", "name": "groupCollapsed", "meta": { "added": [ "v8.5.0" ], "changes": [] }, "signatures": [ { "params": [] } ], "desc": "

An alias for console.group().

" }, { "textRaw": "console.groupEnd()", "type": "method", "name": "groupEnd", "meta": { "added": [ "v8.5.0" ], "changes": [] }, "signatures": [ { "params": [] } ], "desc": "

Decreases indentation of subsequent lines by two spaces.

" }, { "textRaw": "console.info([data][, ...args])", "type": "method", "name": "info", "meta": { "added": [ "v0.1.100" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`data` {any}", "name": "data", "type": "any", "optional": true }, { "textRaw": "`...args` {any}", "name": "...args", "type": "any", "optional": true } ] } ], "desc": "

The console.info() function is an alias for console.log().

" }, { "textRaw": "console.log([data][, ...args])", "type": "method", "name": "log", "meta": { "added": [ "v0.1.100" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`data` {any}", "name": "data", "type": "any", "optional": true }, { "textRaw": "`...args` {any}", "name": "...args", "type": "any", "optional": true } ] } ], "desc": "

Prints to stdout with newline. Multiple arguments can be passed, with the\nfirst used as the primary message and all additional used as substitution\nvalues similar to printf(3) (the arguments are all passed to\nutil.format()).

\n
const count = 5;\nconsole.log('count: %d', count);\n// Prints: count: 5, to stdout\nconsole.log('count:', count);\n// Prints: count: 5, to stdout\n
\n

See util.format() for more information.

" }, { "textRaw": "console.table(tabularData[, properties])", "type": "method", "name": "table", "meta": { "added": [ "v10.0.0" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`tabularData` {any}", "name": "tabularData", "type": "any" }, { "textRaw": "`properties` {string[]} Alternate properties for constructing the table.", "name": "properties", "type": "string[]", "desc": "Alternate properties for constructing the table.", "optional": true } ] } ], "desc": "

Try to construct a table with the columns of the properties of tabularData\n(or use properties) and rows of tabularData and log it. Falls back to just\nlogging the argument if it can’t be parsed as tabular.

\n
// These can't be parsed as tabular data\nconsole.table(Symbol());\n// Symbol()\n\nconsole.table(undefined);\n// undefined\n\nconsole.table([{ a: 1, b: 'Y' }, { a: 'Z', b: 2 }]);\n// ┌─────────┬─────┬─────┐\n// │ (index) │  a  │  b  │\n// ├─────────┼─────┼─────┤\n// │    0    │  1  │ 'Y' │\n// │    1    │ 'Z' │  2  │\n// └─────────┴─────┴─────┘\n\nconsole.table([{ a: 1, b: 'Y' }, { a: 'Z', b: 2 }], ['a']);\n// ┌─────────┬─────┐\n// │ (index) │  a  │\n// ├─────────┼─────┤\n// │    0    │  1  │\n// │    1    │ 'Z' │\n// └─────────┴─────┘\n
" }, { "textRaw": "console.time([label])", "type": "method", "name": "time", "meta": { "added": [ "v0.1.104" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`label` {string} **Default:** `'default'`", "name": "label", "type": "string", "default": "`'default'`", "optional": true } ] } ], "desc": "

Starts a timer that can be used to compute the duration of an operation. Timers\nare identified by a unique label. Use the same label when calling\nconsole.timeEnd() to stop the timer and output the elapsed time in\nmilliseconds to stdout. Timer durations are accurate to the sub-millisecond.

" }, { "textRaw": "console.timeEnd([label])", "type": "method", "name": "timeEnd", "meta": { "added": [ "v0.1.104" ], "changes": [ { "version": "v6.0.0", "pr-url": "https://github.com/nodejs/node/pull/5901", "description": "This method no longer supports multiple calls that don’t map to individual `console.time()` calls; see below for details." } ] }, "signatures": [ { "params": [ { "textRaw": "`label` {string} **Default:** `'default'`", "name": "label", "type": "string", "default": "`'default'`", "optional": true } ] } ], "desc": "

Stops a timer that was previously started by calling console.time() and\nprints the result to stdout:

\n
console.time('100-elements');\nfor (let i = 0; i < 100; i++) {}\nconsole.timeEnd('100-elements');\n// prints 100-elements: 225.438ms\n
" }, { "textRaw": "console.timeLog([label][, ...data])", "type": "method", "name": "timeLog", "meta": { "added": [ "v10.7.0" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`label` {string} **Default:** `'default'`", "name": "label", "type": "string", "default": "`'default'`", "optional": true }, { "textRaw": "`...data` {any}", "name": "...data", "type": "any", "optional": true } ] } ], "desc": "

For a timer that was previously started by calling console.time(), prints\nthe elapsed time and other data arguments to stdout:

\n
console.time('process');\nconst value = expensiveProcess1(); // Returns 42\nconsole.timeLog('process', value);\n// Prints \"process: 365.227ms 42\".\ndoExpensiveProcess2(value);\nconsole.timeEnd('process');\n
" }, { "textRaw": "console.trace([message][, ...args])", "type": "method", "name": "trace", "meta": { "added": [ "v0.1.104" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`message` {any}", "name": "message", "type": "any", "optional": true }, { "textRaw": "`...args` {any}", "name": "...args", "type": "any", "optional": true } ] } ], "desc": "

Prints to stderr the string 'Trace: ', followed by the util.format()\nformatted message and stack trace to the current position in the code.

\n
console.trace('Show me');\n// Prints: (stack trace will vary based on where trace is called)\n//  Trace: Show me\n//    at repl:2:9\n//    at REPLServer.defaultEval (repl.js:248:27)\n//    at bound (domain.js:287:14)\n//    at REPLServer.runBound [as eval] (domain.js:300:12)\n//    at REPLServer.<anonymous> (repl.js:412:12)\n//    at emitOne (events.js:82:20)\n//    at REPLServer.emit (events.js:169:7)\n//    at REPLServer.Interface._onLine (readline.js:210:10)\n//    at REPLServer.Interface._line (readline.js:549:8)\n//    at REPLServer.Interface._ttyWrite (readline.js:826:14)\n
" }, { "textRaw": "console.warn([data][, ...args])", "type": "method", "name": "warn", "meta": { "added": [ "v0.1.100" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`data` {any}", "name": "data", "type": "any", "optional": true }, { "textRaw": "`...args` {any}", "name": "...args", "type": "any", "optional": true } ] } ], "desc": "

The console.warn() function is an alias for console.error().

" } ], "signatures": [ { "params": [ { "textRaw": "`stdout` {stream.Writable}", "name": "stdout", "type": "stream.Writable" }, { "textRaw": "`stderr` {stream.Writable}", "name": "stderr", "type": "stream.Writable", "optional": true }, { "textRaw": "`ignoreErrors` {boolean} Ignore errors when writing to the underlying streams. **Default:** `true`.", "name": "ignoreErrors", "type": "boolean", "default": "`true`", "desc": "Ignore errors when writing to the underlying streams.", "optional": true } ], "desc": "

Creates a new Console with one or two writable stream instances. stdout is a\nwritable stream to print log or info output. stderr is used for warning or\nerror output. If stderr is not provided, stdout is used for stderr.

\n
const output = fs.createWriteStream('./stdout.log');\nconst errorOutput = fs.createWriteStream('./stderr.log');\n// custom simple logger\nconst logger = new Console({ stdout: output, stderr: errorOutput });\n// use it like console\nconst count = 5;\nlogger.log('count: %d', count);\n// in stdout.log: count 5\n
\n

The global console is a special Console whose output is sent to\nprocess.stdout and process.stderr. It is equivalent to calling:

\n
new Console({ stdout: process.stdout, stderr: process.stderr });\n
" }, { "params": [ { "textRaw": "`options` {Object}", "name": "options", "type": "Object", "options": [ { "textRaw": "`stdout` {stream.Writable}", "name": "stdout", "type": "stream.Writable" }, { "textRaw": "`stderr` {stream.Writable}", "name": "stderr", "type": "stream.Writable" }, { "textRaw": "`ignoreErrors` {boolean} Ignore errors when writing to the underlying streams. **Default:** `true`.", "name": "ignoreErrors", "type": "boolean", "default": "`true`", "desc": "Ignore errors when writing to the underlying streams." }, { "textRaw": "`colorMode` {boolean|string} Set color support for this `Console` instance. Setting to `true` enables coloring while inspecting values, setting to `'auto'` will make color support depend on the value of the `isTTY` property and the value returned by `getColorDepth()` on the respective stream. **Default:** `'auto'`.", "name": "colorMode", "type": "boolean|string", "default": "`'auto'`", "desc": "Set color support for this `Console` instance. Setting to `true` enables coloring while inspecting values, setting to `'auto'` will make color support depend on the value of the `isTTY` property and the value returned by `getColorDepth()` on the respective stream." } ] } ], "desc": "

Creates a new Console with one or two writable stream instances. stdout is a\nwritable stream to print log or info output. stderr is used for warning or\nerror output. If stderr is not provided, stdout is used for stderr.

\n
const output = fs.createWriteStream('./stdout.log');\nconst errorOutput = fs.createWriteStream('./stderr.log');\n// custom simple logger\nconst logger = new Console({ stdout: output, stderr: errorOutput });\n// use it like console\nconst count = 5;\nlogger.log('count: %d', count);\n// in stdout.log: count 5\n
\n

The global console is a special Console whose output is sent to\nprocess.stdout and process.stderr. It is equivalent to calling:

\n
new Console({ stdout: process.stdout, stderr: process.stderr });\n
" } ] } ], "modules": [ { "textRaw": "Inspector only methods", "name": "inspector_only_methods", "desc": "

The following methods are exposed by the V8 engine in the general API but do\nnot display anything unless used in conjunction with the inspector\n(--inspect flag).

", "methods": [ { "textRaw": "console.markTimeline([label])", "type": "method", "name": "markTimeline", "meta": { "added": [ "v8.0.0" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`label` {string} **Default:** `'default'`", "name": "label", "type": "string", "default": "`'default'`", "optional": true } ] } ], "desc": "

This method does not display anything unless used in the inspector. The\nconsole.markTimeline() method is the deprecated form of\nconsole.timeStamp().

" }, { "textRaw": "console.profile([label])", "type": "method", "name": "profile", "meta": { "added": [ "v8.0.0" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`label` {string}", "name": "label", "type": "string", "optional": true } ] } ], "desc": "

This method does not display anything unless used in the inspector. The\nconsole.profile() method starts a JavaScript CPU profile with an optional\nlabel until console.profileEnd() is called. The profile is then added to\nthe Profile panel of the inspector.

\n
console.profile('MyLabel');\n// Some code\nconsole.profileEnd('MyLabel');\n// Adds the profile 'MyLabel' to the Profiles panel of the inspector.\n
" }, { "textRaw": "console.profileEnd([label])", "type": "method", "name": "profileEnd", "meta": { "added": [ "v8.0.0" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`label` {string}", "name": "label", "type": "string", "optional": true } ] } ], "desc": "

This method does not display anything unless used in the inspector. Stops the\ncurrent JavaScript CPU profiling session if one has been started and prints\nthe report to the Profiles panel of the inspector. See\nconsole.profile() for an example.

\n

If this method is called without a label, the most recently started profile is\nstopped.

" }, { "textRaw": "console.timeStamp([label])", "type": "method", "name": "timeStamp", "meta": { "added": [ "v8.0.0" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`label` {string}", "name": "label", "type": "string", "optional": true } ] } ], "desc": "

This method does not display anything unless used in the inspector. The\nconsole.timeStamp() method adds an event with the label 'label' to the\nTimeline panel of the inspector.

" }, { "textRaw": "console.timeline([label])", "type": "method", "name": "timeline", "meta": { "added": [ "v8.0.0" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`label` {string} **Default:** `'default'`", "name": "label", "type": "string", "default": "`'default'`", "optional": true } ] } ], "desc": "

This method does not display anything unless used in the inspector. The\nconsole.timeline() method is the deprecated form of console.time().

" }, { "textRaw": "console.timelineEnd([label])", "type": "method", "name": "timelineEnd", "meta": { "added": [ "v8.0.0" ], "changes": [] }, "signatures": [ { "params": [ { "textRaw": "`label` {string} **Default:** `'default'`", "name": "label", "type": "string", "default": "`'default'`", "optional": true } ] } ], "desc": "

This method does not display anything unless used in the inspector. The\nconsole.timelineEnd() method is the deprecated form of\nconsole.timeEnd().

" } ], "type": "module", "displayName": "Inspector only methods" } ], "type": "module", "displayName": "Console" } ] }