Open In App

Node.js console.trace() Method

Last Updated : 05 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The console.trace() method is an inbuilt application programming interface of the console module which is used to print stack trace messages to stderr in a newline. Similar to console.error() method. 

Syntax:

console.trace(message, args);

Parameters: This method has two parameters as mentioned above and described below:

  • message: This parameter specifies the message to be printed.
  • args: It is an optional parameter that specifies the parameters to be passed as substitution values in the message. All passed args are sent to util.format().

Return Value: This method doesn’t return anything but print the ‘Trace:’ string followed by the formatted message to stderr in a newline and stack trace to the current position in the code. 

Example 1: The below examples illustrate the use of console.trace() method in Node.js.

Filename: app.js 

javascript




// Node.js program to demonstrate the
// console.trace() method
 
// Accessing console module
const console = require('console');
 
// Calling console.trace() method
console.trace("stack teace sample");
 
console.trace(
    "stack trace sample with args: %d", 39);


Run the app.js file using the following command:

node app.js

Output:

Trace: stack teace sample
at Object. (C:\nodejs\g\console\console_trace.js:4:9)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
Trace: stack trace sample with args: 39
at Object. (C:\nodejs\g\console\console_trace.js:5:9)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

Example 2: The below examples illustrate the use of the console.trace() method in Node.js. 

Filename: app.js 

javascript




// Node.js program to demonstrate the
// console.trace() method
 
// Accessing console module
const console = require('console');
 
// Calling console.trace() method
console.trace("stack trace message: "
               + "at %s: line no: %d ", "ff()", 96);
 
let isTrace = true;
 
console.custom_trace = function (message) {
    if (isTrace) {
        console.trace(message);
    }
}
 
console.custom_trace("custom trace message");


Run the app.js file using the following command:

node app.js

Output:

Trace: stack trace message: at ff(): line no: 96
at Object. (C:\nodejs\g\console\console_trace_1.js:4:9)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
Trace: custom trace message
at Console.console.custom_trace (C:\nodejs\g\console\console_trace_1.js:11:13)
at Object. (C:\nodejs\g\console\console_trace_1.js:14:9)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

Note: The above program will compile and run by using the node filename.js command. 

Reference: https://nodejs.org/api/console.html#console_console_trace_data_args



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads