×

Node.js Core Debugger

Node.js – Core Debugger: It’s always a challenge when it comes to debug an application. The Asynchronous workflows of Node.js include an additional layer of intricacy to this difficult process. Even though some updates have been made to the JavaScript V8 engine to create access to asynchronous stack traces a bit easier.

Usually, we get errors on the main thread of the applications, making the debugging process a little bit harder. Whenever there is a crash in our Node.js applications, we normally rely on some types of complicated CLI tooling for the analysis of the core dumps.

There are several tools available to debug Node.js applications, which include the following:

  • Core Node.js Debugger
  • Node Inspector
  • IDEs Built-in Debugger

Let’s discuss these tools in brief.

Core Node.js Debugger

Node.js provides a non-graphic built-in debugging utility. It can be used on multiple platforms and provides several commands necessary to debug a Node.js application.

Let’s see a simple Node.js application as follows:

File: mydebug.js

var fs = require('fs');
fs.readFile('newfile.txt', 'utf8', function (err, data) {
    debugger;
    if (err) throw err;
    console.log(data);
});

Inserting debugger in the JavaScript code would let the user to stop the debugger. For example, in the above snippet of code, we have to write a debugger inside the callback function to check the "data" parameter.

Now, to initialize the core debugger of Node.js, start Node.js command-line and type the following command:

$ node <space> inspect <space> script_file.js

In the above command, typing the inspect argument followed by the script's path would let the user to debug the addressed script file. In addition to that, a prompt will be appeared, representing a successful initiative of the debugger.

Here’s an illustration of what we have discussed so far.

$ node inspect mydebug.js
< Debugger listening on ws://12
< 7.0.0.1:9229/7be1f7d9-62aa-4dd3-bee1-ca71011cac4a
< For help, see: https://nodejs.org/en/docs/inspector
< Debugger attached.
Break on start in mydebug.js:1
> 1 var fs = require('fs');
  2
  3 fs.readFile('newfile.txt', 'utf8', function (err, data) {
debug>
Node.js Core Debugger

In the above output, the > symbol indicates the current debugging statement. To continue debugging and move to the next statement, use next.

debug> next
break in mydebug.js:3
  1 var fs = require('fs');
  2
> 3 fs.readFile('newfile.txt', 'utf8', function (err, data) {
  4
  5     debugger;
debug>
Node.js Core Debugger

Now, the breakpoint of the debugger is in line: 3. To directly stop the execution to “debugger”, if any, use the cont command.

debug> cont
break in mydebug.js:5
  3 fs.readFile('newfile.txt', 'utf8', function (err, data) {
  4
> 5     debugger;
  6
  7     if (err) throw err;
debug>
Node.js Core Debugger

As we can see, our script file has a “debugger” on the fifth line of code; the cont command breaks the debugging at line: 5. Moreover, it is feasible to watch the variable values or expressions while debugging using the watch(‘my_expression’) command. To print the active watcher, type watchers. We can also remove a watcher using unwatch(‘my_expression’). Here’s an output for the same.

debug> watch('data')
debug> watchers
  0: data = 'Welcome to TUTORIALANDEXAMPLE.com'
Node.js Core Debugger

Just like the above, there are various important commands available in the core debugger of Node.js. Some of them are listed below with their descriptions:

CommandDescription
nextThe next command allows the user to step on the next line for debugging
contThe cont command allows the user to continue the code's execution until the next debugger present in the code.
watch()The watch() command starts to watch an expression or variable values.
watchersThe 'watchers' command prints the active watcher.
stepThe step command allows the user to step into the function.
outThe out command allows the user to step out of the function.
pauseThe pause command allows the user to pause the debugging process.

Related Topics

Node.js Events Module

Introduction to the Node.js events Module As we’ve already discussed, Node.js is a JavaScript runtime with an event-driven architecture designed to perform Asynchronous tasks. In order to accomplish the event-driven architecture,...

4 minutes read.

Node.js Path Module

Understanding the path Module in Node.js The path module is a built-in module of Node.js that provides various methods to interact with the files and directory path names on the device’s...

6 minutes read.

Node.js Console

Node.js Console: Like the JavaScript console mechanism, a console module is designed for Node.js that provides a debugging console for writing or printing messages. Usually, in JavaScript, a console mechanism is...

4 minutes read.

Difference Between Node.js and Django

Node.js vs Django In Web Application Development, we must have a robust technology to provide strength to our Web application. Node.js and Django both are pretty commanding technologies that help us...

6 minutes read.

Node.js Tutorial

Introduction to Node.js: Node.js is a non-proprietary and platform-independent runtime environment for implementing JavaScript code outside of a browser. However, NodeJS isn't a framework and not a programming language. We...

4 minutes read.

Differentiation of Web Scraping and Web Crawling

Most of us get confused between the word’s web scraping and web crawling, and even some people think they are synonyms for each other. But there is a considerable difference between...

3 minutes read.

Node.js Event Loop

An Introduction to Event Loop As we have already discussed, Node.js is an Asynchronous platform with a non-blocking I/O. Node.js has awesome features that made it this thriving, and the Event...

5 minutes read.

Node.js REPL

Node.js REPL: REPL represents the Read Eval Print Loop. REPL is modest; much more, an interactive software development environment similar to the command line shells used in various Operating Systems....

4 minutes read.

Node.js HTTP Module

An Introduction to HTTP Module The http module is a core module in Node.js that uses the Hypertext Transfer Protocol (HTTP) to transfer data. With the help of the http module,...

7 minutes read.

Node.js Parsing Arguments

Understanding Command-Line Arguments The Strings of text used to transfer extra information to a program through the command-line interface (CLI) while execution of the application, is known as Command-Line arguments. These...

9 minutes read.

Node.js Modules

Node.js Modules: At the point when we'll begin composing Node.js applications, we would place all our code into one big node.js file, regardless of how enormous or complex our application...

4 minutes read.

Node.js Buffers

An Introduction to Buffers A Buffer is an area in memory (typically RAM) that is used to store binary data. Binary is a collection or a set of 0 and 1....

4 minutes read.

How to Install Node.js on Windows?

Environment Setup for Node.js Since Node.js is a mandatory section of the program that runs JavaScript, similar to all other required section of programs that run a programming language, it is...

4 minutes read.

Node.js Core Debugger

Node.js – Core Debugger: It’s always a challenge when it comes to debug an application. The Asynchronous workflows of Node.js include an additional layer of intricacy to this difficult process....

3 minutes read.

Node.js Streams

An Introduction to the Streams Streams are one of the elemental concepts that are introduced to power the applications based on Node.js. These are the method used to handle the data...

6 minutes read.

Node.js NPM

Node.js – Node Package Manager NPM is an abbreviation for Node Package Manager. It is the default package manager developed by Isaac Z. Schlueter using JavaScript Programming Language, designed for JavaScript Runtime...

7 minutes read.

Node.js OS Module

Introduction to Node.js OS Module The OS Module in Node.js provides the Operating System-related Utilities. This module delivers the functions to interact with the Operating System. It also delivers the Operating...

5 minutes read.

Node.js Callback Concept

Understanding the Concept of Callback A Callback is an asynchronous equivalent for a function that is called at the completion of a task. The Callback helps to avoid any I/O blocking and allows other code to execute...

2 minutes read.

Node.js Discovering Timers

Node.js Discovering Timers: Timers are multi-lingual, and much functional in countless use cases. The Timers are mostly used in many popular programming languages, including Node.js. However, it's pretty simple to...

4 minutes read.

Difference Between Node.js and PHP

Node.js vs PHP Node.js and PHP both are the powerful back-ends to create versatile websites. Even when these two technologies fall under the same category, their features are pretty diverse. There's...

6 minutes read.