- Node.js Tutorial
- Node.js - Home
- Node.js - Introduction
- Node.js - Environment Setup
- Node.js - First Application
- Node.js - REPL Terminal
- Node.js - Command Line Options
- Node.js - Package Manager (NPM)
- Node.js - Callbacks Concept
- Node.js - Upload Files
- Node.js - Send an Email
- Node.js - Events
- Node.js - Event Loop
- Node.js - Event Emitter
- Node.js - Debugger
- Node.js - Global Objects
- Node.js - Console
- Node.js - Process
- Node.js - Scaling Application
- Node.js - Packaging
- Node.js - Express Framework
- Node.js - RESTFul API
- Node.js - Buffers
- Node.js - Streams
- Node.js - File System
- Node.js MySQL
- Node.js - MySQL Get Started
- Node.js - MySQL Create Database
- Node.js - MySQL Create Table
- Node.js - MySQL Insert Into
- Node.js - MySQL Select From
- Node.js - MySQL Where
- Node.js - MySQL Order By
- Node.js - MySQL Delete
- Node.js - MySQL Update
- Node.js - MySQL Join
- Node.js MongoDB
- Node.js - MongoDB Get Started
- Node.js - MongoDB Create Database
- Node.js - MongoDB Create Collection
- Node.js - MongoDB Insert
- Node.js - MongoDB Find
- Node.js - MongoDB Query
- Node.js - MongoDB Sort
- Node.js - MongoDB Delete
- Node.js - MongoDB Update
- Node.js - MongoDB Limit
- Node.js - MongoDB Join
- Node.js Modules
- Node.js - Modules
- Node.js - Built-in Modules
- Node.js - Utility Modules
- Node.js - Web Module
- Node.js Useful Resources
- Node.js - Quick Guide
- Node.js - Useful Resources
- Node.js - Dicussion
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)
To Continue Learning Please Login
Login with Google