How to print a stack trace in Node.js?

Sometimes, we want to print a stack trace in Node.js.

In this article, we’ll look at how to print a stack trace in Node.js.

How to print a stack trace in Node.js?

To print a stack trace in Node.js, we can get the stack property from the Error instance.

For instance, we write

const {
  stack
} = new Error()
console.log(stack)

to get the stack trace from the stack property of the Error instance.

We can also use console.trace method with no argument to print the stack trace.

Conclusion

To print a stack trace in Node.js, we can get the stack property from the Error instance.