0
0
Node.jsframework~10 mins

Node.js built-in debugger in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Node.js built-in debugger
Start Node.js with --inspect
Debugger listens on port
Connect debugger client
Set breakpoints
Run code until breakpoint
Pause execution
Inspect variables & call stack
Step through code (step in/out/over)
Resume or stop debugging
This flow shows how Node.js debugger starts, listens, connects, pauses, inspects, steps, and resumes code execution.
Execution Sample
Node.js
node --inspect-brk app.js
// In debugger client:
break app.js:5
cont
next
repl
Starts Node.js with debugger, sets breakpoint at line 5, continues, steps next, and opens REPL for inspection.
Execution Table
StepDebugger ActionCode Execution StateVariables StateOutput/Effect
1Start Node.js with --inspect-brkExecution paused before first lineNo variables initializedDebugger listening on ws://127.0.0.1:9229/uuid
2Connect debugger clientPaused at first lineNo variables initializedClient connected, ready to send commands
3Set breakpoint at app.js:5Breakpoint setNo variables initializedBreakpoint registered at line 5
4Continue execution (cont)Runs until line 5Variables initialized up to line 4Execution paused at breakpoint line 5
5Inspect variablesPaused at line 5Variables visible with current valuesVariables shown in debugger UI
6Step over (next)Executes line 5, pauses at line 6Variables updated if changedExecution paused at line 6
7Open REPLPaused at line 6Variables accessibleUser can evaluate expressions interactively
8Resume executionRuns to end or next breakpointVariables update as code runsProgram continues running
9Exit debuggerProgram runs normallyVariables as per program endDebugger detached
💡 Debugger session ends when user detaches or program finishes execution
Variable Tracker
VariableStartAfter Step 4After Step 6Final
countundefined3410
messageundefined'start''processing''done'
Key Moments - 3 Insights
Why does the program pause immediately when using --inspect-brk?
Because --inspect-brk tells Node.js to pause execution before running any code, allowing you to set breakpoints before the program starts (see execution_table step 1).
How can I see the current value of a variable while paused?
Use the debugger's inspect or REPL commands to evaluate variables at the paused line, as shown in execution_table steps 5 and 7.
What is the difference between 'next' and 'cont' commands?
'cont' continues running until the next breakpoint or program end, while 'next' steps over the current line and pauses at the next line (see steps 4 and 6).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does the debugger pause at the user-set breakpoint?
AStep 6
BStep 2
CStep 4
DStep 1
💡 Hint
Check the 'Code Execution State' column for when execution pauses at line 5.
According to variable_tracker, what is the value of 'count' after step 6?
A3
B4
Cundefined
D10
💡 Hint
Look at the 'After Step 6' column for variable 'count'.
If you remove the --inspect-brk flag, what changes in the execution flow?
AProgram runs immediately until breakpoint
BProgram pauses before first line
CDebugger does not listen on any port
DCannot set breakpoints
💡 Hint
Compare step 1 description about --inspect-brk pausing before first line.
Concept Snapshot
Node.js built-in debugger:
- Start with 'node --inspect' or '--inspect-brk' to enable debugging
- Connect debugger client (Chrome DevTools, VSCode)
- Set breakpoints to pause execution
- Use 'cont' to continue, 'next' to step over lines
- Inspect variables and call stack while paused
- Use REPL for interactive evaluation
- Resume or detach to end debugging
Full Transcript
The Node.js built-in debugger allows you to pause and inspect your code while it runs. You start Node.js with the --inspect or --inspect-brk flag. The --inspect-brk flag pauses execution before the first line, letting you set breakpoints early. Then you connect a debugger client like Chrome DevTools or VSCode. You set breakpoints where you want the program to pause. When the program hits a breakpoint, it pauses, letting you inspect variables and the call stack. You can step through code line by line using commands like 'next' or continue running with 'cont'. The REPL lets you run commands and check variable values interactively. When done, you resume or detach the debugger to let the program finish normally.