Sophie

Sophie

distrib > Mageia > 6 > x86_64 > media > core-updates > by-pkgid > 4642cf16cdfb16e1cdaefd0999c41911 > files > 35

nodejs-docs-6.17.1-8.mga6.noarch.rpm

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Console | Node.js v6.17.1 Documentation</title>
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic">
  <link rel="stylesheet" href="assets/style.css">
  <link rel="stylesheet" href="assets/sh.css">
  <link rel="canonical" href="https://nodejs.org/api/console.html">
</head>
<body class="alt apidoc" id="api-section-console">
  <div id="content" class="clearfix">
    <div id="column2" class="interior">
      <div id="intro" class="interior">
        <a href="/" title="Go back to the home page">
          Node.js
        </a>
      </div>
      <ul>
<li><a class="nav-documentation" href="documentation.html">About these Docs</a></li>
<li><a class="nav-synopsis" href="synopsis.html">Usage &amp; Example</a></li>
</ul>
<div class="line"></div>

<ul>
<li><a class="nav-assert" href="assert.html">Assertion Testing</a></li>
<li><a class="nav-buffer" href="buffer.html">Buffer</a></li>
<li><a class="nav-addons" href="addons.html">C++ Addons</a></li>
<li><a class="nav-n-api" href="n-api.html">C/C++ Addons - N-API</a></li>
<li><a class="nav-child_process" href="child_process.html">Child Processes</a></li>
<li><a class="nav-cluster" href="cluster.html">Cluster</a></li>
<li><a class="nav-cli" href="cli.html">Command Line Options</a></li>
<li><a class="nav-console active" href="console.html">Console</a></li>
<li><a class="nav-crypto" href="crypto.html">Crypto</a></li>
<li><a class="nav-debugger" href="debugger.html">Debugger</a></li>
<li><a class="nav-dns" href="dns.html">DNS</a></li>
<li><a class="nav-domain" href="domain.html">Domain</a></li>
<li><a class="nav-errors" href="errors.html">Errors</a></li>
<li><a class="nav-events" href="events.html">Events</a></li>
<li><a class="nav-fs" href="fs.html">File System</a></li>
<li><a class="nav-globals" href="globals.html">Globals</a></li>
<li><a class="nav-http" href="http.html">HTTP</a></li>
<li><a class="nav-https" href="https.html">HTTPS</a></li>
<li><a class="nav-intl" href="intl.html">Internationalization</a></li>
<li><a class="nav-modules" href="modules.html">Modules</a></li>
<li><a class="nav-net" href="net.html">Net</a></li>
<li><a class="nav-os" href="os.html">OS</a></li>
<li><a class="nav-path" href="path.html">Path</a></li>
<li><a class="nav-process" href="process.html">Process</a></li>
<li><a class="nav-punycode" href="punycode.html">Punycode</a></li>
<li><a class="nav-querystring" href="querystring.html">Query Strings</a></li>
<li><a class="nav-readline" href="readline.html">Readline</a></li>
<li><a class="nav-repl" href="repl.html">REPL</a></li>
<li><a class="nav-stream" href="stream.html">Stream</a></li>
<li><a class="nav-string_decoder" href="string_decoder.html">String Decoder</a></li>
<li><a class="nav-timers" href="timers.html">Timers</a></li>
<li><a class="nav-tls" href="tls.html">TLS/SSL</a></li>
<li><a class="nav-tty" href="tty.html">TTY</a></li>
<li><a class="nav-dgram" href="dgram.html">UDP/Datagram</a></li>
<li><a class="nav-url" href="url.html">URL</a></li>
<li><a class="nav-util" href="util.html">Utilities</a></li>
<li><a class="nav-v8" href="v8.html">V8</a></li>
<li><a class="nav-vm" href="vm.html">VM</a></li>
<li><a class="nav-zlib" href="zlib.html">ZLIB</a></li>
</ul>
<div class="line"></div>

<ul>
<li><a class="nav-https-github-com-nodejs-node" href="https://github.com/nodejs/node">GitHub Repo &amp; Issue Tracker</a></li>
<li><a class="nav-http-groups-google-com-group-nodejs" href="http://groups.google.com/group/nodejs">Mailing List</a></li>
</ul>

    </div>

    <div id="column1" data-id="console" class="interior">
      <header>
        <h1>Node.js v6.17.1 Documentation</h1>
        <div id="gtoc">
          <ul>
            <li>
              <a href="index.html" name="toc">Index</a>
            </li>
            <li>
              <a href="all.html">View on single page</a>
            </li>
            <li>
              <a href="console.json">View as JSON</a>
            </li>
            
    <li class="version-picker">
      <a href="#">View another version <span>&#x25bc;</span></a>
      <ol class="version-picker"><li><a href="https://nodejs.org/docs/latest-v9.x/api/console.html">9.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v8.x/api/console.html">8.x <b>LTS</b></a></li>
<li><a href="https://nodejs.org/docs/latest-v7.x/api/console.html">7.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v6.x/api/console.html">6.x <b>LTS</b></a></li>
<li><a href="https://nodejs.org/docs/latest-v5.x/api/console.html">5.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v4.x/api/console.html">4.x <b>LTS</b></a></li>
<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/console.html">0.12.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/console.html">0.10.x</a></li></ol>
    </li>
  
          </ul>
        </div>
        <hr>
      </header>

      <div id="toc">
        <h2>Table of Contents</h2>
        <ul>
<li><span class="stability_2"><a href="#console_console">Console</a></span><ul>
<li><span class="stability_undefined"><a href="#console_class_console">Class: Console</a></span><ul>
<li><span class="stability_undefined"><a href="#console_new_console_stdout_stderr">new Console(stdout[, stderr])</a></span></li>
<li><span class="stability_undefined"><a href="#console_console_assert_value_message_args">console.assert(value[, message][, ...args])</a></span></li>
<li><span class="stability_undefined"><a href="#console_console_clear">console.clear()</a></span></li>
<li><span class="stability_undefined"><a href="#console_console_count_label">console.count([label])</a></span></li>
<li><span class="stability_undefined"><a href="#console_console_countreset_label_default">console.countReset([label = &#39;default&#39;])</a></span></li>
<li><span class="stability_undefined"><a href="#console_console_dir_obj_options">console.dir(obj[, options])</a></span></li>
<li><span class="stability_undefined"><a href="#console_console_error_data_args">console.error([data][, ...args])</a></span></li>
<li><span class="stability_undefined"><a href="#console_console_info_data_args">console.info([data][, ...args])</a></span></li>
<li><span class="stability_undefined"><a href="#console_console_log_data_args">console.log([data][, ...args])</a></span></li>
<li><span class="stability_undefined"><a href="#console_console_time_label">console.time(label)</a></span></li>
<li><span class="stability_undefined"><a href="#console_console_timeend_label">console.timeEnd(label)</a></span></li>
<li><span class="stability_undefined"><a href="#console_console_trace_message_args">console.trace(message[, ...args])</a></span></li>
<li><span class="stability_undefined"><a href="#console_console_warn_data_args">console.warn([data][, ...args])</a></span></li>
</ul>
</li>
</ul>
</li>
</ul>

      </div>

      <div id="apicontent">
        <h1>Console<span><a class="mark" href="#console_console"id="console_console">#</a></span></h1>
<!--introduced_in=v0.10.13-->
<div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p>The <code>console</code> module provides a simple debugging console that is similar to the
JavaScript console mechanism provided by web browsers.</p>
<p>The module exports two specific components:</p>
<ul>
<li>A <code>Console</code> class with methods such as <code>console.log()</code>, <code>console.error()</code> and
<code>console.warn()</code> that can be used to write to any Node.js stream.</li>
<li>A global <code>console</code> instance configured to write to <a href="process.html#process_process_stdout"><code>process.stdout</code></a> and
<a href="process.html#process_process_stderr"><code>process.stderr</code></a>. The global <code>console</code> can be used without calling
<code>require(&#39;console&#39;)</code>.</li>
</ul>
<p><strong><em>Warning</em></strong>: The global console object&#39;s methods are neither consistently
synchronous like the browser APIs they resemble, nor are they consistently
asynchronous like all other Node.js streams. See the <a href="process.html#process_a_note_on_process_i_o">note on process I/O</a> for
more information.</p>
<p>Example using the global <code>console</code>:</p>
<pre><code class="lang-js">console.log(&#39;hello world&#39;);
// Prints: hello world, to stdout
console.log(&#39;hello %s&#39;, &#39;world&#39;);
// Prints: hello world, to stdout
console.error(new Error(&#39;Whoops, something bad happened&#39;));
// Prints: [Error: Whoops, something bad happened], to stderr

const name = &#39;Will Robinson&#39;;
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
</code></pre>
<p>Example using the <code>Console</code> class:</p>
<pre><code class="lang-js">const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);

myConsole.log(&#39;hello world&#39;);
// Prints: hello world, to out
myConsole.log(&#39;hello %s&#39;, &#39;world&#39;);
// Prints: hello world, to out
myConsole.error(new Error(&#39;Whoops, something bad happened&#39;));
// Prints: [Error: Whoops, something bad happened], to err

const name = &#39;Will Robinson&#39;;
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err
</code></pre>
<h2>Class: Console<span><a class="mark" href="#console_class_console"id="console_class_console">#</a></span></h2>
<!--type=class-->
<p>The <code>Console</code> class can be used to create a simple logger with configurable
output streams and can be accessed using either <code>require(&#39;console&#39;).Console</code>
or <code>console.Console</code>:</p>
<pre><code class="lang-js">const Console = require(&#39;console&#39;).Console;
</code></pre>
<pre><code class="lang-js">const Console = console.Console;
</code></pre>
<h3>new Console(stdout[, stderr])<span><a class="mark" href="#console_new_console_stdout_stderr"id="console_new_console_stdout_stderr">#</a></span></h3>
<p>Creates a new <code>Console</code> with one or two writable stream instances. <code>stdout</code> is a
writable stream to print log or info output. <code>stderr</code> is used for warning or
error output. If <code>stderr</code> is not provided, <code>stdout</code> is used for <code>stderr</code>.</p>
<pre><code class="lang-js">const output = fs.createWriteStream(&#39;./stdout.log&#39;);
const errorOutput = fs.createWriteStream(&#39;./stderr.log&#39;);
// custom simple logger
const logger = new Console(output, errorOutput);
// use it like console
const count = 5;
logger.log(&#39;count: %d&#39;, count);
// in stdout.log: count 5
</code></pre>
<p>The global <code>console</code> is a special <code>Console</code> whose output is sent to
<a href="process.html#process_process_stdout"><code>process.stdout</code></a> and <a href="process.html#process_process_stderr"><code>process.stderr</code></a>. It is equivalent to calling:</p>
<pre><code class="lang-js">new Console(process.stdout, process.stderr);
</code></pre>
<h3>console.assert(value[, message][, ...args])<span><a class="mark" href="#console_console_assert_value_message_args"id="console_console_assert_value_message_args">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.101</span>
</div><p>A simple assertion test that verifies whether <code>value</code> is truthy. If it is not,
an <code>AssertionError</code> is thrown. If provided, the error <code>message</code> is formatted
using <a href="util.html#util_util_format_format_args"><code>util.format()</code></a> and used as the error message.</p>
<pre><code class="lang-js">console.assert(true, &#39;does nothing&#39;);
// OK
console.assert(false, &#39;Whoops %s&#39;, &#39;didn\&#39;t work&#39;);
// AssertionError: Whoops didn&#39;t work
</code></pre>
<p><em>Note: the <code>console.assert()</code> method is implemented differently in Node.js
than the <code>console.assert()</code> method <a href="https://developer.mozilla.org/en-US/docs/Web/API/console/assert">available in browsers</a>.</em></p>
<p>Specifically, in browsers, calling <code>console.assert()</code> with a falsy
assertion will cause the <code>message</code> to be printed to the console without
interrupting execution of subsequent code. In Node.js, however, a falsy
assertion will cause an <code>AssertionError</code> to be thrown.</p>
<p>Functionality approximating that implemented by browsers can be implemented
by extending Node.js&#39; <code>console</code> and overriding the <code>console.assert()</code> method.</p>
<p>In the following example, a simple module is created that extends and overrides
the default behavior of <code>console</code> in Node.js.</p>
<pre><code class="lang-js">&#39;use strict&#39;;

// Creates a simple extension of console with a
// new impl for assert without monkey-patching.
const myConsole = Object.create(console, {
  assert: {
    value(assertion, message, ...args) {
      try {
        console.assert(assertion, message, ...args);
      } catch (err) {
        console.error(err.stack);
      }
    },
    configurable: true,
    enumerable: true,
    writable: true,
  },
});

module.exports = myConsole;
</code></pre>
<p>This can then be used as a direct replacement for the built in console:</p>
<pre><code class="lang-js">const console = require(&#39;./myConsole&#39;);
console.assert(false, &#39;this message will print, but no error thrown&#39;);
console.log(&#39;this will also print&#39;);
</code></pre>
<h3>console.clear()<span><a class="mark" href="#console_console_clear"id="console_console_clear">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v6.13.0</span>
</div><p>When <code>stdout</code> is a TTY, calling <code>console.clear()</code> will attempt to clear the
TTY. When <code>stdout</code> is not a TTY, this method does nothing.</p>
<p><em>Note</em>: The specific operation of <code>console.clear()</code> can vary across operating
systems and terminal types. For most Linux operating systems, <code>console.clear()</code>
operates similarly to the <code>clear</code> shell command. On Windows, <code>console.clear()</code>
will clear only the output in the current terminal viewport for the Node.js
binary.</p>
<h3>console.count([label])<span><a class="mark" href="#console_console_count_label"id="console_console_count_label">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v6.13.0</span>
</div><ul>
<li><code>label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> The display label for the counter. Defaults to <code>&#39;default&#39;</code>.</li>
</ul>
<p>Maintains an internal counter specific to <code>label</code> and outputs to <code>stdout</code> the
number of times <code>console.count()</code> has been called with the given <code>label</code>.</p>
<!-- eslint-skip -->
<pre><code class="lang-js">&gt; console.count()
default: 1
undefined
&gt; console.count(&#39;default&#39;)
default: 2
undefined
&gt; console.count(&#39;abc&#39;)
abc: 1
undefined
&gt; console.count(&#39;xyz&#39;)
xyz: 1
undefined
&gt; console.count(&#39;abc&#39;)
abc: 2
undefined
&gt; console.count()
default: 3
undefined
&gt;
</code></pre>
<h3>console.countReset([label = &#39;default&#39;])<span><a class="mark" href="#console_console_countreset_label_default"id="console_console_countreset_label_default">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v6.13.0</span>
</div><ul>
<li><code>label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a> The display label for the counter. Defaults to <code>&#39;default&#39;</code>.</li>
</ul>
<p>Resets the internal counter specific to <code>label</code>.</p>
<!-- eslint-skip -->
<pre><code class="lang-js">&gt; console.count(&#39;abc&#39;);
abc: 1
undefined
&gt; console.countReset(&#39;abc&#39;);
undefined
&gt; console.count(&#39;abc&#39;);
abc: 1
undefined
&gt;
</code></pre>
<!-- eslint-enable -->
<h3>console.dir(obj[, options])<span><a class="mark" href="#console_console_dir_obj_options"id="console_console_dir_obj_options">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.101</span>
</div><ul>
<li><code>obj</code> <span class="type">&lt;any&gt;</span></li>
<li><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&lt;Object&gt;</a><ul>
<li><code>showHidden</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&lt;boolean&gt;</a></li>
<li><code>depth</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&lt;number&gt;</a></li>
<li><code>colors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&lt;boolean&gt;</a></li>
</ul>
</li>
</ul>
<p>Uses <a href="util.html#util_util_inspect_object_options"><code>util.inspect()</code></a> on <code>obj</code> and prints the resulting string to <code>stdout</code>.
This function bypasses any custom <code>inspect()</code> function defined on <code>obj</code>. An
optional <code>options</code> object may be passed to alter certain aspects of the
formatted string:</p>
<ul>
<li><p><code>showHidden</code> - if <code>true</code> then the object&#39;s non-enumerable and symbol
properties will be shown too. Defaults to <code>false</code>.</p>
</li>
<li><p><code>depth</code> - tells <a href="util.html#util_util_inspect_object_options"><code>util.inspect()</code></a> how many times to recurse while
formatting the object. This is useful for inspecting large complicated objects.
Defaults to <code>2</code>. To make it recurse indefinitely, pass <code>null</code>.</p>
</li>
<li><p><code>colors</code> - if <code>true</code>, then the output will be styled with ANSI color codes.
Defaults to <code>false</code>. Colors are customizable; see
<a href="util.html#util_customizing_util_inspect_colors">customizing <code>util.inspect()</code> colors</a>.</p>
</li>
</ul>
<h3>console.error([data][, ...args])<span><a class="mark" href="#console_console_error_data_args"id="console_console_error_data_args">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.100</span>
</div><p>Prints to <code>stderr</code> with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to <a href="http://man7.org/linux/man-pages/man3/printf.3.html">printf(3)</a> (the arguments are all passed to
<a href="util.html#util_util_format_format_args"><code>util.format()</code></a>).</p>
<pre><code class="lang-js">const code = 5;
console.error(&#39;error #%d&#39;, code);
// Prints: error #5, to stderr
console.error(&#39;error&#39;, code);
// Prints: error 5, to stderr
</code></pre>
<p>If formatting elements (e.g. <code>%d</code>) are not found in the first string then
<a href="util.html#util_util_inspect_object_options"><code>util.inspect()</code></a> is called on each argument and the resulting string
values are concatenated. See <a href="util.html#util_util_format_format_args"><code>util.format()</code></a> for more information.</p>
<h3>console.info([data][, ...args])<span><a class="mark" href="#console_console_info_data_args"id="console_console_info_data_args">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.100</span>
</div><p>The <code>console.info()</code> function is an alias for <a href="#console_console_log_data_args"><code>console.log()</code></a>.</p>
<h3>console.log([data][, ...args])<span><a class="mark" href="#console_console_log_data_args"id="console_console_log_data_args">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.100</span>
</div><p>Prints to <code>stdout</code> with newline. Multiple arguments can be passed, with the
first used as the primary message and all additional used as substitution
values similar to <a href="http://man7.org/linux/man-pages/man3/printf.3.html">printf(3)</a> (the arguments are all passed to
<a href="util.html#util_util_format_format_args"><code>util.format()</code></a>).</p>
<pre><code class="lang-js">const count = 5;
console.log(&#39;count: %d&#39;, count);
// Prints: count: 5, to stdout
console.log(&#39;count:&#39;, count);
// Prints: count: 5, to stdout
</code></pre>
<p>See <a href="util.html#util_util_format_format_args"><code>util.format()</code></a> for more information.</p>
<h3>console.time(label)<span><a class="mark" href="#console_console_time_label"id="console_console_time_label">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.104</span>
</div><ul>
<li><code>label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul>
<p>Starts a timer that can be used to compute the duration of an operation. Timers
are identified by a unique <code>label</code>. Use the same <code>label</code> when you call
<a href="#console_console_timeend_label"><code>console.timeEnd()</code></a> to stop the timer and output the elapsed time in
milliseconds to <code>stdout</code>. Timer durations are accurate to the sub-millisecond.</p>
<h3>console.timeEnd(label)<span><a class="mark" href="#console_console_timeend_label"id="console_console_timeend_label">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.104</span>
</div><ul>
<li><code>label</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&lt;string&gt;</a></li>
</ul>
<p>Stops a timer that was previously started by calling <a href="#console_console_time_label"><code>console.time()</code></a> and
prints the result to <code>stdout</code>:</p>
<pre><code class="lang-js">console.time(&#39;100-elements&#39;);
for (let i = 0; i &lt; 100; i++) ;
console.timeEnd(&#39;100-elements&#39;);
// prints 100-elements: 225.438ms
</code></pre>
<p><em>Note: As of Node.js v6.0.0, <code>console.timeEnd()</code> deletes the timer to avoid
leaking it. On older versions, the timer persisted. This allowed
<code>console.timeEnd()</code> to be called multiple times for the same label. This
functionality was unintended and is no longer supported.</em></p>
<h3>console.trace(message[, ...args])<span><a class="mark" href="#console_console_trace_message_args"id="console_console_trace_message_args">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.104</span>
</div><p>Prints to <code>stderr</code> the string <code>&#39;Trace :&#39;</code>, followed by the <a href="util.html#util_util_format_format_args"><code>util.format()</code></a>
formatted message and stack trace to the current position in the code.</p>
<pre><code class="lang-js">console.trace(&#39;Show me&#39;);
// Prints: (stack trace will vary based on where trace is called)
//  Trace: Show me
//    at repl:2:9
//    at REPLServer.defaultEval (repl.js:248:27)
//    at bound (domain.js:287:14)
//    at REPLServer.runBound [as eval] (domain.js:300:12)
//    at REPLServer.&lt;anonymous&gt; (repl.js:412:12)
//    at emitOne (events.js:82:20)
//    at REPLServer.emit (events.js:169:7)
//    at REPLServer.Interface._onLine (readline.js:210:10)
//    at REPLServer.Interface._line (readline.js:549:8)
//    at REPLServer.Interface._ttyWrite (readline.js:826:14)
</code></pre>
<h3>console.warn([data][, ...args])<span><a class="mark" href="#console_console_warn_data_args"id="console_console_warn_data_args">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v0.1.100</span>
</div><p>The <code>console.warn()</code> function is an alias for <a href="#console_console_error_data_args"><code>console.error()</code></a>.</p>

      </div>
    </div>
  </div>
  <script src="assets/sh_main.js"></script>
  <script src="assets/sh_javascript.min.js"></script>
  <script>highlight(undefined, undefined, 'pre');</script>
  <!-- __TRACKING__ -->
</body>
</html>