Sridhar Raj Sampath Kumar Archive

Printing stack trace in nodejs

Debugging an issue can be hard if we don't get the stack trace. Stack trace saves us the time by taking us right to the line number inside a file where the error occurs. Just logging error with plain console.log won't work.

It logs [TypeError: user.getName is not a function]

We got a TypeError when we called a non existing function in the user object. Since user object will be used in multiple files across our application, error type alone will be insufficient. We can enhance this by printing stack to get the exact location of the code causing an issue.

With err.stack, we now know that the code at line number 3 of index.js causes the issue.