The Node.js Debugger is used to identify and troubleshoot errors. It helps developers pause program execution, inspect variables, set breakpoints and execute code step by step to understand how the program is working.
The Node.js debugger is a built-in debugging capability. It allows us to inspect, pause and step through our JavaScript code line by line during runtime to identify and fix errors. Node.js provides various built-in tools to help troubleshoot and debug applications.
We can use the Node.js debugger in the following cases:
Error detection: The Node.js debugger is helpful for developers to identify and troubleshoot errors. It allows them to stop execution, inspect variables and analyze the state of the program while it is running.
Inspect Variables: It permits us to check the values of variables while the program is running.
Set breakpoints: We can pause execution at specific lines of code to inspect the program's behavior and analyze program flow.
Track program execution: We can follow the code step by step to understand how the program executes and how each statement is executed.
Debug asynchronous operations: It assists developers identify and troubleshoot issues in callbacks, Promises and async/await operations.
The commonly used Node.js debugging options are given below:
The --inspect flag activates the V8 inspector in Node.js that lets us to debug our server-side JavaScript code using debugging tools such as Chrome DevTools or Visual Studio Code. The Node.js Inspector listens on 127.0.0.1:9229 by default.
Code:
const http = require ("http");
const server = http.createServer((req, res) => {
const message = "Hello, Node.js!" ;
console.log(message);
res.end(message);
});
server.listen(3000, () => {
console.log("Server running on port 3000");
});
Save the code as app.js and run it with:
node --inspect app.js
Output:
Debugger listening on ws://127.0.0.1:9229… Server running on port 3000
Explanation:
The --inspect flag starts the Node.js Inspector on the default debugging address 127.0.0.1:9229. We can then connect a debugging client such as Chrome DevTools to inspect the running application, set breakpoints and examine variables.
The --inspect-brk flag starts the debugger and pauses execution before the user code begins executing. It permits developers to attach a debugger before any application code is executed.
Syntax
node --inspect-brk app.js
Code:
function calculateTotal(price, tax) {
const subtotal = price * 2;
const total = subtotal + tax;
return total;
}
console.log(calculateTotal(100, 5));
Output:
205
Explanation:
In the above example, we have defined a function called calculateTotal() that doubles the given price, adds the tax to it and returns the total value which is then displayed in the console as 205.
We can debug Node.js applications using tools such as Chrome DevTools and Visual Studio Code to inspect code, set breakpoints and identify errors during execution.
Debugging Node.js with Chrome DevTools involves utilizing Chrome DevTools to inspect and debug Node.js applications, set breakpoints, examine variables and identify issues during execution.
Code:
function addNumbers(num1, num2) {
debugger;
return num1 + num2;
}
console.log(addNumbers(4, 4));
Output:
8
Explanation:
The debugger statement pauses execution when the application is being debugged which allows developers to inspect the code and variables. The function then returns the sum of 4 and 4 which is 8.
Debugging with VS Code helps developers to identify, inspect and fix errors by running step-by-step and examining variables during execution. Let us see an example to understand this.
Code:
function addNumbers(num1, num2) {
debugger;
return num1 + num2;
}
console.log(addNumbers(6, 6));
Output:
12
Explanation:
In this example, the debugger statement pauses execution when the application is running under the VS Code debugger which allows developers to inspect the values of num1 and num2 before the function returns their sum.
The JavaScript debugger statement acts as a programmatic breakpoint. When encountered while a debugger is active then it pauses program execution and permits developers to inspect the code and variables.
Code:
function calculateTotalPrice(price, tax) {
let subtotal = price * 1.05;
debugger;
let total = subtotal + tax;
return total;
}
console.log(calculateTotalPrice(100, 5));
Output:
110
Explanation:
We first calculate the subtotal by increasing the price by 5%. The debugger statement pauses execution. It allows us to inspect the value of subtotal. The tax is then added to the subtotal to calculate the total price.
Node.js provides the built-in inspect command to start the debugger. Open the terminal in your project directory and run the command below:
node inspect app.js
The above command starts the Node.js debugger and lets us debug the app.js file using debugger commands such as next, step and cont. Now you can use the debugger commands to control program execution and inspect your code.
The difference between --inspect and --inspect-brk is given in the table below:
| Feature | --inspect | --inspect-brk |
|---|---|---|
| Initial Execution | The application starts executing immediately. | Execution pauses before the first line of user code. |
| Debugger Behavior | The application runs without waiting for a debugger to attach. | Execution pauses at startup until the debugger resumes the program. |
| Best uses | Debugging applications while they are running such as web servers. | Debugging startup code or short-lived scripts where execution begins immediately. |
Node.js supports multiple commands to control the debugger. These commands allow us to navigate through various parts of the script execution and inspect values at different stages. Let us understand these commands with the help of the table below:
| Command | Description |
|---|---|
| cont(or c) | This command continues program execution until the next breakpoint or until the program ends. |
| next(or n) | This command helps to move to the next line of code. |
| step(or s) | It steps into a function to inspect its execution line by line. |
| out (or o) | It steps out of the current function. |
| pause | It pauses the execution manually. |
| watch('var') | It watches the specific variable for changes. |
| repl | It opens the debugging REPL to execute JavaScript commands. |
We request you to subscribe our newsletter for upcoming updates.