Multiple ways to log the printstack trace in Nodejs

In Nodejs, There are multiple ways to print an error stack trace in nodejs.

Stacktrace is displayed when an unexpected error by a nodejs javascript engine. It helps developers to debug and fix the issues.

Stacktrace gives a method line number call stack trace in Last In First Out (LIFO).

How to Print error stack trace in Nodejs?

There are multiple ways we can do it in javascript and nodejs

  • using try and catch

try and catch provides used to catch exceptions in the code.

Code wrapped in try block Here is an try catch example in javascript

try {
  throw new Error("Custom Error");
} catch (error) {
  console.log(error);
}

Output:

A:\work\nodework>node error.js
Error: Custom Error
at Object.<anonymous> (A:\work\nodework\error.js:11:11)
at Module.\_compile (internal/modules/cjs/loader.js:1068:30)
at Object.Module.\_extensions..js (internal/modules/cjs/loader.js:1097:10)
at Module.load (internal/modules/cjs/loader.js:933:32)
at Function.Module.\_load (internal/modules/cjs/loader.js:774:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47
  • Error stack property

stack property contains the string version of the stack trace.

stack property contains a stack trace of a method called trace in LIFO order.

First, Create a Error object, and object.stack return string

console.log(new Error("custom error").stack);
  • Console trace function

Console object in javascript provides various functions to log the information, debug and error information.

trace() method in console logs stack trace of a method calls of a given request.

syntax

console.trace(new Error("My Error"));

Output:

A:\work\nodework>node error.js
Trace: Error: My Error
at Object.<anonymous> (A:\work\nodework\error.js:16:15)
at Module.\_compile (internal/modules/cjs/loader.js:1068:30)
at Object.Module.\_extensions..js (internal/modules/cjs/loader.js:1097:10)
at Module.load (internal/modules/cjs/loader.js:933:32)
at Function.Module.\_load (internal/modules/cjs/loader.js:774:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47
at Object.<anonymous> (A:\work\nodework\error.js:16:9)
at Module.\_compile (internal/modules/cjs/loader.js:1068:30)
at Object.Module.\_extensions..js (internal/modules/cjs/loader.js:1097:10)
at Module.load (internal/modules/cjs/loader.js:933:32)
at Function.Module.\_load (internal/modules/cjs/loader.js:774:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47
  • using process unhandledRejection event

In Nodejs Application, There is an global event unhandledRejection on process object which can be written to handle error and promise errors in a chain

process.on('unhandledRejection', function(err, promise) {
    console.error('Unhandled rejection',error);
});
or

process.on('unhandledRejection', ((err,promise) => {
    console.error('Unhandled rejection',error);
});

  • use captureStackTrace function in error

Error captureStackTrace function adds a stack property to an target object returns string of an stack trace.

const myemp = { id: 11, name: "Eric" };
Error.captureStackTrace(myemp);
console.log(myemp.stack);

Output:

A:\work\nodework>node error.js
john
at Object.<anonymous> (A:\work\nodework\error.js:2:7)
at Module.\_compile (internal/modules/cjs/loader.js:1068:30)
at Object.Module.\_extensions..js (internal/modules/cjs/loader.js:1097:10)
at Module.load (internal/modules/cjs/loader.js:933:32)
at Function.Module.\_load (internal/modules/cjs/loader.js:774:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47