如何在 Node.js 中打印堆栈跟踪?

Does anyone know how to print a stack trace in Node.js?

转载于:https://stackoverflow.com/questions/2923858/how-to-print-a-stack-trace-in-node-js

9个回答

As already answered, you can simply use the trace command:

console.trace("I am here");

However, if you came to this question searching about how to log the stack trace of an exception, you can simply log the Exception object.

try {  
  // if something unexpected
  throw new Error("Something unexpected has occurred.");     

} catch (e) {
  console.error(e);
}

It will log:

Error: Something unexpected has occurred.
    at main (c:\Users\Me\Documents\MyApp\app.js:9:15)
    at Object. (c:\Users\Me\Documents\MyApp\app.js:17:1)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3


If your Node.js version is < than 6.0.0, logging the Exception object will not be enough. In this case, it will print only:

[Error: Something unexpected has occurred.]

For Node version < 6, use console.error(e.stack) instead of console.error(e) to print the error message plus the full stack, like the current Node version does.


Note: if the exception is created as a string like throw "myException", it's not possible to retrieve the stack trace and logging e.stack yields undefined.

To be safe, you can use

console.error(e.stack || e);

and it will work for old and new Node.js versions.

csdnceshi70
笑故挽风 you save my day)
大约一年之前 回复
csdnceshi52
妄徒之命 sorry I just discovered something crucial. See my comment against the related post: stackoverflow.com/questions/42528677/…. It seems that logging the error on its own does indeed show the whole content of the error, but trying to concatenate it (like a string) with other text will cause only the brief message part to be used. It all makes a lot more sense with that realisation.
2 年多之前 回复
weixin_41568183
零零乙 try installing n to test different Node versions in your machine or see this test with Node 7.4 and compare it with Node 0.8. I could not get a permalink for the last one, but run console.error(new Error("test")) to see the difference. Regarding a reference about this change in behavior, I couldn't get one in Node change logs. It seems to be a V8 change, but I also couldn't confirm.
2 年多之前 回复
csdnceshi52
妄徒之命 do you have a reference for this apparent change in behaviour? I'm still seeing (with node 6.9.4) that console.error(e) only prints the brief message. And I suspect that it's because it prints a toString representation of e, and toString for e only returns the message and nothing else: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
2 年多之前 回复
weixin_41568183
零零乙 confirmed that this behavior changed on version 6.0.0
2 年多之前 回复
weixin_41568183
零零乙 thank you for pointing this out. It seems that behavior has changed between Node versions 4.x and 7.x (probably a V8 change). I've updated my answer.
2 年多之前 回复
csdnceshi52
妄徒之命 Won't console.error(e) print everything in the e object, including e.stack?
2 年多之前 回复

Any Error object has a stack member that traps the point at which it was constructed.

var stack = new Error().stack
console.log( stack )

or more simply:

console.trace("Here I am!")
csdnceshi51
旧行李 console.log(err.stack) and console.trace() do not give you same results. Whereas err.stack gives you the stack trace for the err object itself (functioning the way we all normally think of exceptions), console.trace() will print out the call stack at the point where console.trace() is being called. So if you catch some error being thrown by some deeper layer of code, console.trace() will not contain that deeper layer code in the stack trace since that code is no longer on the stack. However, console.log(err.stack) will contain the deeper layers as long as it threw an Error object.
6 年多之前 回复
csdnceshi63
elliott.david One advantage of trace is it shows the current line/context as well which stack does not. The info is in the error object if you want to manually create that line I guess.
大约 7 年之前 回复
csdnceshi55
~Onlooker +1 for also showing new Error().stack, which works in cases where you don't want to involve the console.
大约 7 年之前 回复
csdnceshi53
Lotus@ As of now, sys is depricated. It is replaced by 'util'.
8 年多之前 回复
weixin_41568208
北城已荒凉 or just sys.puts(new Error().stack) (after adding the system module)
大约 9 年之前 回复

Now there's a dedicated function on console for that:

console.trace()
weixin_41568126
乱世@小熊 By default this will only show 10 frames, you can use the command-line argument to increase this, e.g. --stack_trace_limit=200
3 年多之前 回复
weixin_41568131
10.24 Just make sure to heed the above comment about console.trace().
大约 5 年之前 回复

With a readily available Node module, it is possible to get full-length stack traces out of Node (albeit with a minor performance penalty): http://www.mattinsler.com/post/26396305882/announcing-longjohn-long-stack-traces-for-node-js

csdnceshi76
斗士狗 Link dead but one may peruse the archived copy here or check out the library.
一年多之前 回复

For what I know printing the complete stack trace in nodejs is not possible, you can just print a "partial" stack trace, you can not see from where you came from in the code, just where the Exception occur. That's what Ryan Dahl explains in this youtube video. http://youtu.be/jo_B4LTHi3I at min 56:30 for being precise. Hope this helps

csdnceshi60
℡Wang Yan true, but the module in @Timboudreau's answer "fixes" that
4 年多之前 回复

Try Error.captureStackTrace(targetObject[, constructorOpt]).

const myObj = {};
function c() {
  // pass
}

function b() {
    Error.captureStackTrace(myObj)
    c()
} 

function a() {
    b()
}

a()

console.log(myObj.stack)

The function a and b are captured in error stack and stored in myObj.

csdnceshi55
~Onlooker If you want an error to have a stack property, you'll need to call this if Node >= 6: Error.captureStackTrace(error).
大约一年之前 回复

To print stacktrace of Error in console in more readable way:

console.log(ex, ex.stack.split("\n"));

Example result:

[Error] [ 'Error',
  '    at repl:1:7',
  '    at REPLServer.self.eval (repl.js:110:21)',
  '    at Interface.<anonymous> (repl.js:239:12)',
  '    at Interface.EventEmitter.emit (events.js:95:17)',
  '    at Interface._onLine (readline.js:202:10)',
  '    at Interface._line (readline.js:531:8)',
  '    at Interface._ttyWrite (readline.js:760:14)',
  '    at ReadStream.onkeypress (readline.js:99:10)',
  '    at ReadStream.EventEmitter.emit (events.js:98:17)',
  '    at emitKey (readline.js:1095:12)' ]

If you want to only log the stack trace of the error (and not the error message) Node 6 and above automatically includes the error name and message inside the stack trace, which is a bit annoying if you want to do some custom error handling:

console.log(error.stack.replace(error.message, ''))

This workaround will log only the error name and stack trace (so you can, for example, format the error message and display it how you want somewhere else in your code).

The above example would print only the error name follow by the stack trace, for example:

Error: 
    at /Users/cfisher/Git/squashed/execProcess.js:6:17
    at ChildProcess.exithandler (child_process.js:213:5)
    at emitTwo (events.js:106:13)
    at ChildProcess.emit (events.js:191:7)
    at maybeClose (internal/child_process.js:877:16)
    at Socket.<anonymous> (internal/child_process.js:334:11)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at Pipe._handle.close [as _onclose] (net.js:498:12)

Instead of:

Error: Error: Command failed: sh ./commands/getBranchCommitCount.sh HEAD
git: 'rev-lists' is not a git command. See 'git --help'.

Did you mean this?
        rev-list

    at /Users/cfisher/Git/squashed/execProcess.js:6:17
    at ChildProcess.exithandler (child_process.js:213:5)
    at emitTwo (events.js:106:13)
    at ChildProcess.emit (events.js:191:7)
    at maybeClose (internal/child_process.js:877:16)
    at Socket.<anonymous> (internal/child_process.js:334:11)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at Pipe._handle.close [as _onclose] (net.js:498:12)

you can use node-stack-trace module which is a power full module to track call stacks.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!