13

Most APIs in node libraries are asynchronous by design. When an exception is thrown in a callback the stacktrace shows only the call stack starting at process._tickCallback.

I wonder whether there is a trick to show also the stack trace of the function that trigerred the _tickCallback.

1
  • Here is a method you can use (Link to a Gihub Gist). Disadvantages: A few additional lines of code - you have to wrap all async. function calls with try/catch (you can also use promise.catch(...)). Then you create a new Error object and add its stack property to the existing stack. If done for ALL async. function calls you end up with a full stack trace. The speed disadvantage (stack trace creation is expensive) should not matter for async. functions (pls. don't use exceptions for normal control flow).
    – Mörre
    Aug 2, 2017 at 12:11

3 Answers 3

7

In node 8 version appeared async_hooks

trace uses this for async stack traces

6

In Node.js 12, Async Stack Traces come out of the box with flag

--async-stack-traces

Node.js Foundation release post - https://medium.com/@nodejs/introducing-node-js-12-76c41a1b3f3f

1

You can take a look at long stacktraces to see if it fits your needs. The module is called stackups. All you do is install it:

npm install --save stackup

and then require it:

require('stackup');

More info here: https://github.com/groundwater/node-stackup

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.