How to Debug JavaScript in VS Code
· Category: VS Code & Developer Tools
Short answer
VS Code has a built-in debugger that works with Node.js and browsers. You set breakpoints, inspect variables, and step through code without leaving the editor.
Steps
- Open the Run and Debug view on the left sidebar.
- Click "create a launch.json file" and select Node.js or Chrome.
- Set breakpoints by clicking the gutter next to line numbers.
- Press F5 to start debugging and F10 to step over functions.
- Watch variables and the call stack in the Debug sidebar.
Tips
- Use conditional breakpoints that only trigger when an expression is true.
- The Debug Console lets you evaluate expressions while paused.
- Source maps enable debugging of transpiled TypeScript or bundled code.
Common issues
- The debugger may not attach if the port is already in use.
- Ensure your launch configuration points to the correct program entry file.