Node.js console.trace() Method - GeeksforGeeks

13 Jul, 2020
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 optional parameter that specifies the parameters to be passed as substitution values in the message. All passed args are sent to util.format().
message: This parameter specifies the message to be printed.
args: It is 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 ‘Trace:’ string followed by the formatted message to stderr in a newline and stack trace to the current position in the code.
Below examples illustrate the use of console.trace() method in Node.js:
Example 1:Filename: app.js
// Node.js program to demonstrate the // console.trace() method  // Accessing console moduleconst console = require('console');   // Calling console.trace() methodconsole.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 sampleat 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: 39at 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:Filename: app.js
// Node.js program to demonstrate the // console.trace() method  // Accessing console moduleconst console = require('console');   // Calling console.trace() methodconsole.trace("stack trace message: "    + "at %s: line no: %d ", "ff()", 96);   var 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: 96at 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 messageat 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
Node.js-console-module
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Node.js fs.writeFile() Method
Difference between promise and async await in Node.js
How to use an ES6 import in Node.js?
Express.js res.sendFile() Function
Mongoose | findByIdAndUpdate() Function
Top 10 Front End Developer Skills That You Need in 2022
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?
Difference between var, let and const keywords in JavaScript