How to Debug Node.js in VS Code: Simple Steps
launch.json file in the Debug panel to configure the debugger, and start debugging with F5 or the green play button.Why This Happens
Many beginners try to debug Node.js code in VS Code without setting up a proper launch configuration or breakpoints. This causes the debugger to not stop where expected or not start at all.
console.log('Start'); function add(a, b) { return a + b; } console.log(add(2, 3));
The Fix
Set breakpoints by clicking next to the line numbers where you want the code to pause. Then, create a launch.json file in the Debug panel with the Node.js environment configured. This tells VS Code how to run and debug your app.
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/app.js"
}
]
}Prevention
Always create and check your launch.json before debugging. Use breakpoints instead of console.log for better control. Keep VS Code and Node.js updated to avoid compatibility issues.
Related Errors
Debugger not stopping at breakpoints: Check if the program path in launch.json is correct.
Cannot connect to debug target: Make sure no other process is using the debug port.
Source maps not working: Enable source maps in your build if using TypeScript or Babel.