{ "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.
The module exports two specific components:
\nConsole
class with methods such as console.log()
, console.error()
and\nconsole.warn()
that can be used to write to any Node.js stream.console
instance configured to write to process.stdout
and\nprocess.stderr
. The global console
can be used without calling\nrequire('console')
.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.
\nExample using the global console
:
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
\nExample using the Console
class:
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
\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."
}
]
},
"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):
const { Console } = require('console');\n
\nconst { Console } = console;\n
\n",
"methods": [
{
"textRaw": "console.assert(value[, message][, ...args])",
"type": "method",
"name": "assert",
"meta": {
"added": [
"v0.1.101"
],
"changes": []
},
"signatures": [
{
"params": [
{
"textRaw": "`value` {any} ",
"name": "value",
"type": "any"
},
{
"textRaw": "`message` {any} ",
"name": "message",
"type": "any",
"optional": true
},
{
"textRaw": "`...args` {any} ",
"name": "...args",
"type": "any",
"optional": true
}
]
},
{
"params": [
{
"name": "value"
},
{
"name": "message",
"optional": true
},
{
"name": "...args",
"optional": true
}
]
}
],
"desc": "A simple assertion test that verifies whether value
is truthy. If it is not,\nan AssertionError
is thrown. If provided, the error message
is formatted\nusing util.format()
and used as the error message.
console.assert(true, 'does nothing');\n// OK\nconsole.assert(false, 'Whoops %s', 'didn\\'t work');\n// AssertionError: Whoops didn't work\n
\nNote: The console.assert()
method is implemented differently in Node.js\nthan the console.assert()
method available in browsers.
Specifically, in browsers, calling console.assert()
with a falsy\nassertion will cause the message
to be printed to the console without\ninterrupting execution of subsequent code. In Node.js, however, a falsy\nassertion will cause an AssertionError
to be thrown.
Functionality approximating that implemented by browsers can be implemented\nby extending Node.js' console
and overriding the console.assert()
method.
In the following example, a simple module is created that extends and overrides\nthe default behavior of console
in Node.js.
'use strict';\n\n// Creates a simple extension of console with a\n// new impl for assert without monkey-patching.\nconst myConsole = Object.create(console, {\n assert: {\n value: function assert(assertion, message, ...args) {\n try {\n console.assert(assertion, message, ...args);\n } catch (err) {\n console.error(err.stack);\n }\n },\n configurable: true,\n enumerable: true,\n writable: true,\n },\n});\n\nmodule.exports = myConsole;\n
\nThis can then be used as a direct replacement for the built in console:
\nconst console = require('./myConsole');\nconsole.assert(false, 'this message will print, but no error thrown');\nconsole.log('this will also print');\n
\n"
},
{
"textRaw": "console.clear()",
"type": "method",
"name": "clear",
"meta": {
"added": [
"v8.3.0"
],
"changes": []
},
"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.
Note: The specific operation of console.clear()
can vary across operating\nsystems and 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.
Maintains an internal counter specific to label
and outputs to stdout
the\nnumber of times console.count()
has been called with the given label
.
> 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
\n"
},
{
"textRaw": "console.countReset([label='default'])",
"type": "method",
"name": "countReset",
"meta": {
"added": [
"v8.3.0"
],
"changes": []
},
"signatures": [
{
"params": [
{
"textRaw": "`label` {string} The display label for the counter. Defaults to `'default'`. ",
"name": "label",
"type": "string",
"desc": "The display label for the counter. Defaults to `'default'`.",
"optional": true,
"default": "'default'"
}
]
},
{
"params": [
{
"name": "label",
"optional": true,
"default": "'default'"
}
]
}
],
"desc": "Resets the internal counter specific to label
.
> console.count('abc');\nabc: 1\nundefined\n> console.countReset('abc');\nundefined\n> console.count('abc');\nabc: 1\nundefined\n>\n
\n"
},
{
"textRaw": "console.debug(data[, ...args])",
"type": "method",
"name": "debug",
"meta": {
"added": [
"v8.0.0"
],
"changes": []
},
"signatures": [
{
"params": [
{
"textRaw": "`data` {any} ",
"name": "data",
"type": "any"
},
{
"textRaw": "`...args` {any} ",
"name": "...args",
"type": "any",
"optional": true
}
]
},
{
"params": [
{
"name": "data"
},
{
"name": "...args",
"optional": true
}
]
}
],
"desc": "The console.debug()
function is an alias for console.log()
.
Uses util.inspect()
on obj
and prints the resulting string to stdout
.\nThis function bypasses any custom inspect()
function defined on obj
. An\noptional options
object may be passed to alter certain aspects of the\nformatted string:
showHidden
- if true
then the object's non-enumerable and symbol\nproperties will be shown too. Defaults to false
.
depth
- tells util.inspect()
how many times to recurse while\nformatting the object. This is useful for inspecting large complicated objects.\nDefaults to 2
. To make it recurse indefinitely, pass null
.
colors
- if true
, then the output will be styled with ANSI color codes.\nDefaults to false
. Colors are customizable; see\ncustomizing util.inspect()
colors.
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()
).
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
\nIf 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.
Increases indentation of subsequent lines by two spaces.
\nIf one or more label
s are provided, those are printed first without the\nadditional indentation.
An alias for console.group()
.
Decreases indentation of subsequent lines by two spaces.
\n", "signatures": [ { "params": [] } ] }, { "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 } ] }, { "params": [ { "name": "data", "optional": true }, { "name": "...args", "optional": true } ] } ], "desc": "The console.info()
function is an alias for console.log()
.
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()
).
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
\nSee util.format()
for more information.
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.
Stops a timer that was previously started by calling console.time()
and\nprints the result to stdout
:
console.time('100-elements');\nfor (let i = 0; i < 100; i++) {}\nconsole.timeEnd('100-elements');\n// prints 100-elements: 225.438ms\n
\nNote: As of Node.js v6.0.0, console.timeEnd()
deletes the timer to avoid\nleaking it. On older versions, the timer persisted. This allowed\nconsole.timeEnd()
to be called multiple times for the same label. This\nfunctionality was unintended and is no longer supported.
Prints to stderr
the string 'Trace :'
, followed by the util.format()
\nformatted message and stack trace to the current position in the code.
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
\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
}
]
},
{
"params": [
{
"name": "data",
"optional": true
},
{
"name": "...args",
"optional": true
}
]
}
],
"desc": "The console.warn()
function is an alias for console.error()
.
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
.
const output = fs.createWriteStream('./stdout.log');\nconst errorOutput = fs.createWriteStream('./stderr.log');\n// custom simple logger\nconst logger = new Console(output, errorOutput);\n// use it like console\nconst count = 5;\nlogger.log('count: %d', count);\n// in stdout.log: count 5\n
\nThe global console
is a special Console
whose output is sent to\nprocess.stdout
and process.stderr
. It is equivalent to calling:
new Console(process.stdout, process.stderr);\n
\n"
},
{
"params": [
{
"name": "stdout"
},
{
"name": "stderr",
"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
.
const output = fs.createWriteStream('./stdout.log');\nconst errorOutput = fs.createWriteStream('./stderr.log');\n// custom simple logger\nconst logger = new Console(output, errorOutput);\n// use it like console\nconst count = 5;\nlogger.log('count: %d', count);\n// in stdout.log: count 5\n
\nThe global console
is a special Console
whose output is sent to\nprocess.stdout
and process.stderr
. It is equivalent to calling:
new Console(process.stdout, process.stderr);\n
\n"
}
]
}
],
"type": "module",
"displayName": "Console"
}
]
}