Node.js - Debugger



The Debugging is an process in the software development, allowing the user to identify and fix the issue. Node.js provides an built-in debugger that enables user to inspect and troubleshoot the applications.

In this tutorial we are going to learn about the debugging and uses which significantly increases the quality and reliability of Node.js applications.

What is Node.js Debugger

The Node.js debugger is an built-in tool that allows the users to pause the execution of the script and inspect variables, expressions to detect the errors. It is accessible using the command line and integrates well with the various debugging tools such as chrome DevTools and visual studio code.

When running the Node.js script, the errors occurs due to various reasons like syntax error, logical errors or runtime issues. By using this debugger we can analyse the script before deploying the applications.

Starting the Node.js Debugger

The Node.js provides the multiple ways to start debugging a script. We can use the built-in inspect command to start the debugger from the terminal.

Let's consider the simple script file named as 'test.js' to proceed further on debugging.

test.js
console.log("Welcome To, TP.!");

Now we are going to start debugging on the file we have created, For starting the debugger, use the following command.

node inspect test.js

This command starts the debugger in the interactive mode, allowing you to pause execution and step through the code.

Alternatively, we can also use the --inspect flag to enable debugging with the Chrome DevTools.

node --inspect test.js

This command starts the debugger and provides the WebSocket URL to open in the Chrome DevTools to inspect the application visually.

In some cases, we may come across to pause the execution at the start of the script, For pausing the script use the following command.

node --inspect-brk test.js

The --inspect-brk flag ensures the execution is halted at the first line, indicating the user to set the breakpoints before the script runs.

Debugger Commands

Node.js provides a various commands to control the debugger. These commands helps to navigate through various parts of the script execution and inspect values at different stages.

Sr.No Commands & Description
1

cont (or c)

Continue execution to the next breakpoint or to the end of the program.

2

next (or n)

Move to the next line of code.

3

step (or s)

Step into a function to inspect its execution.

4

out (or o)

Step out of the current function

5

pause

Pause the execution manually.

6

watch('var')

Watches the specific variable for changes.

7

repl

Open the debugging REPL ro execute JavaScript commands.

The repl command is particularly useful to interact with variables and functions in the current scope, making it easier to test the fixes.

Debugging with Chrome DevTools

Chrome DevTools provides a graphical interface for debugging the Node.js applications. This approach is more useful for the users who prefer a more visual debugging experience.

Steps to Debug Using Chrome DevTools

step − 1:

Run the following command:

node --inspect-brk test.js
step − 2:

Open the chrome and navigate to the chrome:://inspect.

step − 3:

Then, click on the open dedicated DevTools for Node, to locate the running script under the Remote Target.

step − 4:

In the DevTools interface, we can set the breakpoints, inspect variables and step through the execution just like the debugging on client-side JavaScript.

Advertisements