Open In App

What is stacktrace and how to print in node.js ?

Improve
Improve
Like Article
Like
Save
Share
Report

Node.js is an open-source project that can be used for server-side scripting. Node.js is a cross-platform which allows the developers to code without having to worry about the runtime environment. Node.js is widely used for building dynamic, light-weight, and scalable web applications. Node.js provides a JavaScript runtime environment that can be used for both frontend and backend development. However, applications often throw errors or exceptions that must be handled.

A Stack trace is displayed automatically by the JVM to indicate that an error has occurred during the program execution. 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.js can be classified into four broad categories:

  • Standard JavaScript Errors
  • System Errors
  • User-specified Errors
  • Assertion Errors

Node.js supports several mechanisms for propagating and handling errors that occur during program execution. All Standard JavaScript Errors are handled immediately by throwing an error which can be viewed in the stack trace.

There are four methods to print the stack trace in Node.js that are:

  • Using error.stack Property: The error.stack property describes the point in the code at which the Error was instantiated.

    Syntax:

    error.stack

    Example:




    console.log("This program demonstrates "
                + "stack trace in Node.js");
    var err = new Error().stack
    console.log(err);

    
    

    Output:

    This program demonstrates stack trace in Node.js
    Error
        at Object. (/home/cg/root/2523129/main.js:20:11)
        at Module._compile (module.js:570:32)
        at Object.Module._extensions..js (module.js:579:10)
        at Module.load (module.js:487:32)
        at tryModuleLoad (module.js:446:12)
        at Function.Module._load (module.js:438:3)
        at Module.runMain (module.js:604:10)
        at run (bootstrap_node.js:389:7)
        at startup (bootstrap_node.js:149:9)
        at bootstrap_node.js:504:3
    
  • Using Error.captureStackTrace() Method: This method creates a .stack property on obj that returns a string representing the point in the code at which Error.captureStackTrace(obj) was called. The func parameter represents a function which is optional.

    Syntax:
    Error.captureStackTrace(obj, func)

    Example:




    const obj = {};
    Error.captureStackTrace(obj);
    console.log(obj.stack);
      
    // Alternatively
    function MyNewError() {
        Error.captureStackTrace(this, MyNewError);
    }
      
    console.log(new MyNewError().stack);

    
    

    Output:

    Error
        at Object. (/home/cg/root/2523129/main.js:25:13)
        at Module._compile (module.js:570:32)
        at Object.Module._extensions..js (module.js:579:10)
        at Module.load (module.js:487:32)
        at tryModuleLoad (module.js:446:12)
        at Function.Module._load (module.js:438:3)
        at Module.runMain (module.js:604:10)
        at run (bootstrap_node.js:389:7)
        at startup (bootstrap_node.js:149:9)
        at bootstrap_node.js:504:3
    
  • Using try-catch block: It is a mechanism of Error Handling and it is used when a piece of code is surrounded in a try block and throw an error to the catch block. If the error is not handled then the program terminates abruptly.

    Example:




    try {
        throw new Error("Error occurred");  
    }
    catch(e) {
        console.log(e);
    }

    
    

    Output:

    Error
        at Object. (/home/cg/root/2523129/main.js:25:13)
        at Module._compile (module.js:570:32)
        at Object.Module._extensions..js (module.js:579:10)
        at Module.load (module.js:487:32)
        at tryModuleLoad (module.js:446:12)
        at Function.Module._load (module.js:438:3)
        at Module.runMain (module.js:604:10)
        at run (bootstrap_node.js:389:7)
        at startup (bootstrap_node.js:149:9)
        at bootstrap_node.js:504:3
    
  • Using trace command: The console.trace() method is used to display the trace which represents how the code ended up at a certain point.

    Example:




    console.trace("hello world");

    
    

    Output:

    Trace: hello world
        at Object. (/home/cg/root/2523129/main.js:28:9)
        at Module._compile (module.js:570:32)
        at Object.Module._extensions..js (module.js:579:10)
        at Module.load (module.js:487:32)
        at tryModuleLoad (module.js:446:12)
        at Function.Module._load (module.js:438:3)
        at Module.runMain (module.js:604:10)
        at run (bootstrap_node.js:389:7)
        at startup (bootstrap_node.js:149:9)
        at bootstrap_node.js:504:3
    


Last Updated : 31 May, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads