NodeJS Debugger is a instrument accessed by the command line that helps to debug the Node.js code. It offers a approach to set completely different breakpoints to cease this system’s execution at some extent to search out the bug. Node.js Debugger can also be environment friendly to trace the worth of variables that how they alter with the circulation of execution.
Additionally learn: NodeJS Crypto Module – Encrypt and Decrypt Knowledge
Node.js is especially used to create servers, APIs, and net providers, which may be closely coded, with complicated buildings and it’s onerous to debug it. It is vitally overwhelming for a developer to search out which line precisely produces the bug.
The straightforward answer is Node.js Debugger which offers varied instruments to debug the code utilizing the terminal.
For utilizing Node.js Debugger it’s important that you just have Node.js put in in your laptop. For initiating the Node.js Debugger use the under command.
The place file_name is the title of the file you need to examine.
Node.js Debugger Instructions
Node.js Debugger offers varied instructions that are essential to study earlier than understanding the working of Debugger.
- c/cont – This command is used to execute the code until a breakpoint or finish.
- n/subsequent – This command will transfer the execute to the following line.
- s/step – Use to get within a operate.
- o – Use to get out of a operate.
- pause – pause the execution.
These instructions is likely to be troublesome so that you can perceive, however quickly we are going to see a completely working instance that can clear your doubt.
Working of Debugger
For understanding the working of Debugger, let’s create an instance undertaking containing a single file app.js with the next code.
let numbers = [1, 2, 3, 4, 5]; let sumOfNumbers = 0; for (let i = 0; i
To run the appliance, find it contained in the terminal and sort the next command
The output we anticipated is 15.
The output we get is:
Now, we are going to use Debugger to search out the bug in this system.
We see that the execution begins from the highest and as we press n it is going to transfer to the following line.
Now we’ve to make use of the watch technique to trace the variable, i, and sumOfNumbers. This technique takes an argument, a string containing the title of the variable to trace its worth.
As we proceed we see that the worth of each variables modifications as anticipated, in every loop the worth of the array’s subsequent aspect is added to the sumOfNumbers variable.
Let’s run the loop till the worth of sumOfNumbers turns into NaN.
Now, we see that the bug occurs when the worth of variable i turns into 5, and we discover that the array has solely 5 parts for the reason that parts of an array begin with index 0, so the final aspect of the array needs to be at index 4, however our price of i turns into 5 and it tries so as to add the worth of the aspect within the index 5, however no aspect exists at that index so the default worth of the aspect at index 5 is NaN, that’s why the sumOfNumbers turns into NaN.
From the above commentary, we conclude that the worth of i shouldn’t be larger than 4 as it’s the final index of the array, so we simply take away the = image within the for loop and run the code once more.
We get the identical output as anticipated.
Node.js Debugger helps to debug the code straight by utilizing the command line interface, it comes with Node.js, so for utilizing it Node.js needs to be put in. You’ll be able to undergo the line-by-line execution of the code to search out the bug utilizing the debugger. Hope this text lets you perceive the idea of Node.js Debugger.