Example 13: Debugging Basics (VSCode + Node.js)
This example demonstrates how to use the VSCode debugger with Node.js.
It contains an intentional logical bug in sumArray() so you can:
- Set breakpoints
- Step through code
- Inspect variables and the call stack
- See the difference between expected and actual behavior
Files
app.js– Contains the buggysumArray()function and main execution logic
The Bug
In app.js:
function sumArray(arr) {
let total = 0;
// BUG: should be i < arr.length, not i <= arr.length
for (let i = 0; i <= arr.length; i++) {
total += arr[i];
}
return total;
}
Because of i <= arr.length, the loop runs one step too far:
- On the last iteration,
i === arr.length arr[i]isundefinedtotal += undefinedproducesNaN
When you run the script, it will show:
Sum is NaN
instead of the expected Sum is 15.
How to Run
Simple Execution
- Open this folder (
13_debugging_basics) in VSCode. - Run the script from the terminal:
node app.js
You’ll see output showing the bug (NaN result).
Debugging with VSCode
Step 1: Set a Breakpoint
- Open
app.jsin VSCode. - Click in the left margin (gutter) next to line 29 (inside the
sumArrayfunction) to set a red breakpoint dot.
Step 2: Start Debugging
- Press F5 (or go to Run and Debug in the sidebar, then click the play button).
- VSCode will ask you to create a launch configuration. Select “Node.js”.
- This creates a
.vscode/launch.jsonfile automatically.
Alternatively, you can manually create .vscode/launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug Debugging Example",
"skipFiles": ["<node_internals>/**"],
"program": "${workspaceFolder}/docs/05_examples/13_debugging_basics/app.js"
}
]
}
Step 3: Debug!
- With your breakpoint set, press F5 to start debugging.
- Execution will pause at your breakpoint.
-
Now you can:
- Inspect variables: Look at the VARIABLES panel on the left, or hover your mouse over variables in the code
- Step Over (F10): Execute the current line and move to the next
- Step Into (F11): Enter into function calls
- Continue (F5): Run until the next breakpoint
- Watch expressions: Add variables to the WATCH panel to monitor them
- Step through the loop and watch what happens:
- Notice how
iincrements: 0, 1, 2, 3, 4, 5 - When
i === 5(which equalsarr.length),arr[i]isundefined - This is when
totalbecomesNaN
- Notice how
Fixing the Bug
Once you understand the issue, fix it:
for (let i = 0; i < arr.length; i++) {
total += arr[i];
}
After saving:
- Run the script again:
node app.js - The result should now be:
Sum is 15
Or debug it again to see it working correctly!
Key Learning Points
This example demonstrates:
- Syntax vs. logical errors: The code runs without syntax errors, but the logic is wrong
- How the debugger helps: You can see what the code is actually doing, not what you think it’s doing
- Variable inspection: Watch values change in real-time as you step through code
- Call stack: See how functions call each other
Tips
- Multiple breakpoints: Set breakpoints at different locations to pause at various points
- Conditional breakpoints: Right-click a breakpoint to add a condition (e.g., only pause when
i === 5) - Logpoints: Right-click in the gutter and select “Add Logpoint” to log values without modifying code
- Debug Console: Use the DEBUG CONSOLE to evaluate expressions while paused