As a developer, while implementing the applications we will debug the code which we write to make sure that the functionality is working fine by using different tools. In node.js also we have different tools to debug the applications.
The following are different ways to debug node.js applications.
Node.js has provided a built-in debugging client to debug an application. To debug an application with debugging client, we need to start node.js with an inspect argument followed by the path of the script to debug like as shown in the following statement.
Here, inspect is an argument to enable debugging and script_file_name.js is the path of a script file which we want to debug.
Following is the example of enabling a debugger for simpleisexample.js script file which we created in a previous node.js NPM article.
When we execute the above statement, we will get a successful launch of the debugger and the debugging will always start from the starting point of the script like as shown below.
If you observe the above image, we have a greater than (>) symbol to indicate the current debugging statement.
Now, we can continue the script file debugging in node.js by using following commands.
Command | Description |
---|---|
next / n | The debugging will be moved to next statement. |
cont / c | Continue execution and stops at debugger statement if any. |
step / s | Step in function. |
out / o | Step out function. |
pause | Pause running code |
watch | Add expression or variable to watch while debugging. |
watchers | It will print the values of expressions and variables which are added to the watch. |
unwatch | Remove expression or variable from the watch. |
Following is the example of moving the debugging to the next statement using next
command in node.js.
This is how we can debug the code in node.js using built-in debugging client.
Actually, the node.js debugging client is not a full-featured debugger and the code debugging will always start from the starting of the script. In case, if we want to debug from the specific position of code statements, then it’s better to use debugger statement.
By inserting debugger;
statement in the source code of script will enable a breakpoint at the specified position of code.
Now, create a script file called mathoperations.js and write the code like as shown following with debugger;
statement.
var add = 25 + 50;
var sub = 50 - 25;
debugger;
var mul = 10 * 50;
var divsn = 50 / 25;
console.log("Addition Is :" , add);
console.log("Subtraction Is :" , sub);
console.log("Multiplication Is :" , mul);
console.log("Division Is :" , divsn);
If you observe above code, we are performing some arithmetic operations and added a debugger;
statement to enable the breakpoint at required position of code.
Now, to enable the debugging for the above application, run the following command.
Once we execute the above command, the debugger will start and stops at the first line of the code like as shown below.
If you observe the above image, we have a greater than (>) symbol to indicate the current debugging statement.
Now, use cont
or c
statement to stop the execution directly at the position where we defined a debugger;
statement like as shown below.
If you observe the above image after we execute cont
statement the execution has stopped at the position where defined a debugger;
statement.
Now, if we use the next
statement the debugger will be moved to the next statement like as shown below.
This is how we can move the debugging to the next line of code in node.js based on our requirements.
In node.js, we can watch the expression and variable values while debugging using watch('expression') & watchers commands. On every breakpoint, the expressions in the watcher's list will be evaluated in the current context and displayed immediately before the breakpoint’s source code listing.
Following is the example of using watch
command to add variables to an active watchers list.
If you observe the above example, we added three variables (add, sub, mul) to the watcher's list by using watch
command.
Now, we can watch the active watchers list by using watchers
command like as shown below.
Now if we use cont
statement, the execution will stop at next debugger;
statement, if it found otherwise the code execution will be done like as shown below.
If you observe the above result, the code execution has been completed because we didn’t mention any other debugger;
statements in our code except at line 3.
Once we are done with the debugging tasks and if we want to exit from debug mode, then we need to use a .exit
command like as shown below.
This is how we can do the debugging in node.js to cross verify the functionality of node.js applications based on our requirements.
In the next chapters, we will learn how to use the Node Inspector to debug the node.js applications.