0
0
Node.jsframework~5 mins

Node.js built-in debugger in Node.js

Choose your learning style9 modes available
Introduction

The Node.js built-in debugger helps you find and fix problems in your code by letting you pause and check what is happening step-by-step.

When your Node.js program is not working as expected and you want to see where it breaks.
When you want to check the value of variables at different points in your code.
When you want to understand how your code runs step-by-step.
When you want to test small parts of your code interactively.
When you want to improve your code by finding hidden bugs.
Syntax
Node.js
node inspect your_script.js

Run this command in your terminal to start the debugger for your script.

You can also start debugging by adding --inspect or --inspect-brk flags for advanced debugging with Chrome DevTools.

Examples
Starts the debugger for the app.js file in the terminal.
Node.js
node inspect app.js
Starts the debugger and pauses on the first line, allowing you to connect a browser debugger.
Node.js
node --inspect-brk app.js
In the debugger prompt, cont resumes running the program after a pause.
Node.js
debug> cont
(debugger continues running the program)
In the debugger prompt, next runs the next line and pauses again.
Node.js
debug> next
(debugger runs the next line of code)
Sample Program

This simple program adds two numbers. The debugger; line pauses the program when running with the debugger, letting you inspect variables.

Node.js
function add(a, b) {
  debugger;
  return a + b;
}

console.log(add(2, 3));
OutputSuccess
Important Notes

Use the debugger; statement in your code to set breakpoints easily.

In the debugger prompt, commands like cont, next, step, and repl help you control the program.

You can inspect variables by typing their names in the debugger prompt.

Summary

The Node.js built-in debugger helps pause and inspect your code to find bugs.

Start it with node inspect your_script.js or use debugger; in your code.

Use commands like cont and next to control the program flow while debugging.