• Node.js Video Tutorials

NodeJS - console.trace() Method



The Node.js console.trace() method is an inbuilt method of the Console method.

This method prints the stack 'Trace' to the current position of the code followed by the message and substitution values on stderr in a new line. It shows the line of code that called this particular function as well as all other functions that were called before it, including their arguments and return values if any. This method is almost similar to the console.error() method of Node.js.

Now, let's look into the syntax and usage of the console.trace() method of node.js.

Syntax

Following is the syntax of the Node.js console.trace() method −

console.trace(message, …args);

Parameters

This method accepts only two parameters. The same are discussed below.

  • The first parameter message will print on stderr.

  • The second parameter args is an optional parameter, where we can pass substitution values in the message and those values will be passed to the message.

Return value

This method will print the stack with string 'Trace' to the current position of the code followed by the message and substitution values on stderr in a new line.

Example

In the example below, we are calling the Node.js console.trace() method without passing any message and args parameter.

console.trace();

Output

As we can see in the above output, the trace() method printed the stack 'Trace' to the current position of the code. The current position of the code in the above code is displayed as 1:9. Which means console.trace() started at first 1.

Trace
   at Object.<anonymous> (/home/cg/root/639ac5040c88c/main.js:1:71)
   at Module._compile (internal/modules/cjs/loader.js:702:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
   at Module.load (internal/modules/cjs/loader.js:612:32)
   at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
   at Function.Module._load (internal/modules/cjs/loader.js:543:3)
   at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
   at startup (internal/bootstrap/node.js:238:19)
   at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)

Example

In the example below, we are calling the console.trace() method with its first parameter (message).

console.trace("This is an error!");

Output

If we compile and run the above code, we can see the string 'Trace' printed with the message we passed into the method.

Trace: This is an error!
   at Object.<anonymous> (/home/cg/root/639ac5040c88c/main.js:1:71)
   at Module._compile (internal/modules/cjs/loader.js:702:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
   at Module.load (internal/modules/cjs/loader.js:612:32)
   at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
   at Function.Module._load (internal/modules/cjs/loader.js:543:3)
   at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
   at startup (internal/bootstrap/node.js:238:19)
   at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)

Example

In the following example below, we are calling the console.trace() method by passing both the parameters (message, …args) into the method.

console.trace("This is an %s", 'error!');

Output

As we can see in the output below, the substitution values passed into the message and printed the stack of Trace.

Trace: This is an error!
   at Object.<anonymous> (/home/cg/root/639ac5040c88c/main.js:1:71)
   at Module._compile (internal/modules/cjs/loader.js:702:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
   at Module.load (internal/modules/cjs/loader.js:612:32)
   at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
   at Function.Module._load (internal/modules/cjs/loader.js:543:3)
   at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
   at startup (internal/bootstrap/node.js:238:19)
   at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)

Example

In the example below, we are calling the console.trace() method inside the function.

function one(){
   console.trace("This is an error!");
}
one();

Output

As we can see in the output below, it is printing the stack 'Trace' to the current position of the code followed by the message.

Trace: This is an error!
   at one (/home/cg/root/639ad99d5e18e/main.js:2:13)
   at Object.<anonymous> (/home/cg/root/639ad99d5e18e/main.js:4:1)
   at Module._compile (internal/modules/cjs/loader.js:702:30)
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
   at Module.load (internal/modules/cjs/loader.js:612:32)
   at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
   at Function.Module._load (internal/modules/cjs/loader.js:543:3)
   at Function.Module.runMain (internal/modules/cjs/loader.js:744:10)
   at startup (internal/bootstrap/node.js:238:19)
   at bootstrapNodeJSCore (internal/bootstrap/node.js:572:3)
nodejs_console_module.htm
Advertisements