2

Obviously in console core modules(https://nodejs.org/dist/latest-v4.x/docs/api/console.html) of node documentation exist below code:

console.error(new Error('Whoops, something bad happened'));
  // Prints: [Error: Whoops, something bad happened], to stderr

but,when I run test.js which code show below

var err = new Error('a');
console.error(err);

the terminal print the message like:

Error: a at Object. (/Users/suoyong/Express/连接数据库/error.js:1:73) at Module._compile (module.js:556:32) at Object.Module._extensions..js (module.js:565:10) at Module.load (module.js:473:32) at tryModuleLoad (module.js:432:12) at Function.Module._load (module.js:424:3) at Module.runMain (module.js:590:10) at run (bootstrap_node.js:394:7) at startup (bootstrap_node.js:149:9) at bootstrap_node.js:509:3

As you can see, my code is same with the node doc, and yet the result not. Please help me with the tiny question.,

3
  • Where did you see that? I can't find what you claim in the official documentation. Commented Oct 13, 2016 at 14:21
  • @E_net4 nodejs.org/dist/latest-v4.x/docs/api/console.html that you can Ctr+F find Commented Oct 13, 2016 at 14:29
  • Please include that link in your question. Commented Oct 13, 2016 at 14:35

2 Answers 2

1
console.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to stderr

This is not to be interpreted in the literal sense. Not in the latest LTS and stable versions anyway. Printing an error like that will actually print a textual representation of the error object, which was referred as [Error: Whoops, something bad happened] in the documentation. The actual intended behaviour is further clarified in the documentation of Console.error():

If formatting elements (e.g. %d) are not found in the first string then util.inspect() is called on each argument and the resulting string values are concatenated.

On the side of util.inspect(), this method "returns a string representation of object that is primarily useful for debugging". For objects of type Error, this will yield a string containing the error's message and stack trace.

> const txt = util.inspect(new Error("I'm on SO"))
undefined
> txt
'Error: I\'m on SO\n    at repl:1:26\n    at sigintHandlersWrap (vm.js:22:35)\n    at sigintHandlersWrap (vm.js:96:12)\n    at ContextifyScript.Script.runInThisContext (vm.js:21:12)\n    at REPLServer.defaultEval (repl.js:313:29)\n    at bound (domain.js:280:14)\n    at REPLServer.runBound [as eval] (domain.js:293:12)\n    at REPLServer.<anonymous> (repl.js:513:10)\n    at emitOne (events.js:101:20)\n    at REPLServer.emit (events.js:188:7)'
> console.log(txt)
Error: I'm on SO
    at repl:1:26
    at sigintHandlersWrap (vm.js:22:35)
    at sigintHandlersWrap (vm.js:96:12)
    at ContextifyScript.Script.runInThisContext (vm.js:21:12)
    at REPLServer.defaultEval (repl.js:313:29)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.<anonymous> (repl.js:513:10)
    at emitOne (events.js:101:20)
    at REPLServer.emit (events.js:188:7)
Sign up to request clarification or add additional context in comments.

1 Comment

@suoyong No problem, your question was quite reasonable (also don't forget to accept an answer). I wonder if the devs over at the Node.js foundation know about this.
0

I guess that this changed between Node v4 and Node v6.

With v4, the output is as documented; with v6, a stack trace is included in the output, like what you're seeing.

You can work around it by using console.error(err.toString()), which for both versions will output Error: a (so minus any brackets, but if you really want those you can add them, of course).

1 Comment

The documentation in v6.8.0 shows the same thing. Now, it's hard to guess whether this was an old behaviour not updated in the documentation or just a means of explaining that the error object would be printed to stderr.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.