Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print a stack trace in Node.js?

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

like image 661
mike.toString Avatar asked May 27 '10 18:05

mike.toString


People also ask

How do I print a stack trace error?

The printStackTrace() method of Java. lang. Throwable class used to print this Throwable along with other details like class name and line number where the exception occurred means its backtrace. This method prints a stack trace for this Throwable object on the standard error output stream.

What is stack trace in Nodejs?

The stack trace is used to trace the active stack frames at a particular instance during the execution of a program. The stack trace is useful while debugging code as it shows the exact point that has caused an error. Errors in Node.

Which console method can be used to print the stack trace to the point of its execution?

You can use console. trace() to get a quick and easy stack trace to better understand code execution flow.


2 Answers

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!") 
like image 185
isaacs Avatar answered Sep 23 '22 03:09

isaacs


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

console.trace() 
like image 26
Mariusz Nowak Avatar answered Sep 22 '22 03:09

Mariusz Nowak