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

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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    console.trace("hello world");

    chevron_right

    
    

    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
    



My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.