Node.js console.trace() Method
Last Updated :
05 Apr, 2023
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
Similar Reads
Node.js console.assert() Method The console.assert() method is an inbuilt application programming interface of the console module which is used to assert value passed to it as a parameter, i.e. it checks whether the value is true or not, and prints an error message, if provided and failed to assert the value. Syntax: console.asse
2 min read
Node.js console.clear() Method The console.clear() method is used to clear the stdout, when stdout is a TTY (Teletype) i.e. terminal it will attempt to clear the TTY. When stdout is not a TTY, this method does nothing. The console.clear() will work differently across different operating systems and terminal types. For Linux opera
1 min read
Node.js console.count() Method The console.count() method is an inbuilt application programming interface of the console module which is used to count label passed to it as a parameter, by maintaining an internal counter for that specific label. Syntax: console.count(label) Parameters: This method has one parameter as mentioned a
2 min read
Node.js console.countReset() Method The console.countReset() method is an inbuilt application programming interface of the console module which is used to reset the count for the specific label passed to it as a parameter. Syntax: console.countReset( label ); Parameters: This method has one parameter as mentioned above and described b
2 min read
Node.js console.debug() Method The console.debug() method is an inbuilt application programming interface of the console module which is used to print messages to stdout in a newline. Similar to the console.log() method. Syntax: console.debug(data, args); Parameters: This method has two parameters as mentioned above and described
2 min read
Node.js console.dir() Method The console.dir() method is used to get the list of object properties of a specified object. These object properties also have child objects, from which you can inspect for further information. Syntax: console.dir( object ) Parameters: This method accepts single parameter which holds the object elem
1 min read
Node.js console.error() Function The console.error() function from the console class of Node.js is used to display an error message on the console. It prints to stderr with a newline. Syntax:Â console.error([data][, ...args]) Parameter: This function can contain multiple parameters. The first parameter is used for the primary messa
1 min read
Node.js console.info() Method The console.info() method is an inbuilt application programming interface of the console module which is used to print messages to stdout in a newline. It is similar to the console.log() method. Syntax: console.info(data, args); Parameters: This method has two parameters as mentioned above and descr
2 min read
Node.js console.timeLog() Method The console.timeLog() method is an inbuilt function in Nodejs that is used to display the time for each execution. This function is proved to be effective when used in a loop. Syntax: console.log([label][, ...data]) Parameters: This function accepts two or more parameters. Return Value: This method
1 min read
Node.js console.time() Method The console.time() method is the console class of Node.js. It is used to starts a timer that is used to compute the time taken by a piece of code or function. The method console.timeEnd() is used to stop the timer and output the elapsed time in milliseconds to stdout. The timer can be accurate to th
3 min read