Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 40 ยท Debugging

Debugging Node.js and Python

Debug JavaScript with Node.js and Python in VS Code: auto attach, debug terminal, debugpy configurations, virtual environments, pytest tests, web servers and common problems, with practice code.

You know the debugging concepts; now apply them to two popular languages. Both have excellent VS Code support: Node.js through the built-in JavaScript debugger, and Python through the Python extension and its debugpy adapter. This lesson shows the setup, typical configurations and the problems that trip people up.

You need Node.js or Python installed on your own computer to follow the debugger steps. The runnable code on this page is a stand-in for the buggy programs you would debug, with a hand-made trace of what a debugger would show.

Node.js

Quickest start

  1. Open a .js file. Set a breakpoint (F9).
  2. Press F5. With no launch.json, VS Code runs the current file with Node and the built-in debugger.
  3. For output that needs input, or to use npm scripts, open the JavaScript Debug Terminal: Command Palette > Debug: JavaScript Debug Terminal. In that terminal, any command such as node app.js, npm test or npm run dev starts under the debugger automatically. This is often the easiest way to debug a script, test runner or dev server.

Auto Attach

Enable Debug: Toggle Auto Attach and choose Smart (attaches to scripts you run from the integrated terminal, skipping those in node_modules), or Always. Running node app.js in any integrated terminal now stops on breakpoints.

launch.json for Node

JSON
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Run app",
      "type": "node",
      "request": "launch",
      "program": "${workspaceFolder}/src/index.js",
      "env": { "NODE_ENV": "development" },
      "skipFiles": ["<node_internals>/**"]
    },
    {
      "name": "npm run dev",
      "type": "node",
      "request": "launch",
      "runtimeExecutable": "npm",
      "runtimeArgs": ["run", "dev"],
      "console": "integratedTerminal"
    },
    {
      "name": "Jest current file",
      "type": "node",
      "request": "launch",
      "program": "${workspaceFolder}/node_modules/jest/bin/jest.js",
      "args": ["${relativeFile}", "--runInBand"],
      "console": "integratedTerminal"
    }
  ]
}

Notes:

  • --runInBand makes Jest run in one process so breakpoints work.
  • For TypeScript, compile with source maps ("sourceMap": true in tsconfig.json) and set "outFiles": ["${workspaceFolder}/dist/**/*.js"], or use a runner that handles it (for example tsx through runtimeExecutable).
  • Frameworks such as Express work like any program: set a breakpoint in a route handler and call the URL from a browser or curl.
  • Browser debugging: use the chrome or msedge types (pwa-chrome in older versions) with "url": "http://localhost:3000" and "webRoot". Breakpoints in your front-end code then stop inside the browser.

Python

Setup

  1. Install the Python extension (it also installs Pylance and the Python Debugger extension).
  2. Select an interpreter: Command Palette > Python: Select Interpreter. Pick the virtual environment of your project (.venv). The choice is shown in the Status Bar.
  3. Open a .py file, set a breakpoint, press F5 and choose Python File. VS Code runs the current file in the integrated terminal under debugpy.

Create a virtual environment first if needed:

Terminal
python -m venv .venv

launch.json for Python

JSON
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python: current file",
      "type": "debugpy",
      "request": "launch",
      "program": "${file}",
      "console": "integratedTerminal",
      "justMyCode": true
    },
    {
      "name": "Python: module",
      "type": "debugpy",
      "request": "launch",
      "module": "myapp.main",
      "args": ["--verbose"]
    },
    {
      "name": "Flask app",
      "type": "debugpy",
      "request": "launch",
      "module": "flask",
      "env": { "FLASK_APP": "app.py", "FLASK_DEBUG": "1" },
      "args": ["run", "--no-debugger", "--no-reload"]
    },
    {
      "name": "Attach to port",
      "type": "debugpy",
      "request": "attach",
      "connect": { "host": "localhost", "port": 5678 }
    }
  ]
}

justMyCode: false lets you step into library code, which is useful when a third-party package misbehaves. Older tutorials use "type": "python", which is the previous name; use debugpy in new files. The --no-reload flag prevents Flask's reloader from starting a second process that the debugger cannot see; Django needs the same idea with runserver --noreload.

Debugging tests

With pytest installed, open the Testing view (beaker icon), let it discover tests (Python: Configure Tests), and use the Debug Test button beside any test, or the gutter icon in the test file. Failing tests stop at breakpoints without a launch.json.

Python-specific tools

  • Debug Console accepts Python: type len(users) or df.head() when stopped.
  • The Variables view shows objects with their attributes; pandas DataFrames offer a View Value in Data Viewer action.
  • Exception breakpoints: Raised Exceptions stops at every raise; Uncaught Exceptions stops when nothing handles it.
  • Breakpoint on a decorator-free line works; breakpoints inside a lambda need an inline breakpoint.
  • For code run with python -m debugpy --listen 5678 --wait-for-client app.py you can attach from another terminal or a container.

A practice bug and its trace

Here is a classic bug to debug (a function that should return the average of a list but fails for an empty list). The stand-in below reproduces the situation in JavaScript and prints a "what the Variables view would show" trace for each call, so you can practise reasoning before you open a real debugger.

JS Stand-in: trace the bug

Press Run (or Ctrl + Enter) to see the console output here.

No output.

In a real debugger you would set a breakpoint on the return, run with an empty list, and see total = 0 and numbers.length = 0 in the Variables view; the Debug Console expression total / numbers.length evaluates to NaN. That points straight to the division.

Typical problems

Symptom Likely cause and fix
Gray "unverified" breakpoints (Node) Source maps missing or running a different file; set outFiles, rebuild
ModuleNotFoundError in debug but not in terminal (Python) Wrong interpreter selected; use Python: Select Interpreter
Debugger never stops in a web server The server runs in a child process or reloader; disable reload or attach
Program waits for input Use "console": "integratedTerminal"
Steps into library code constantly Enable skipFiles (Node) or justMyCode (Python)
Breakpoint stops, but values look "optimized away" (C/C++/compiled) Build with debug symbols and without optimization

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the JavaScript Debug Terminal do?

    In that terminal, node scripts and npm commands are auto-attached to the debugger.
  2. 2. Which type should new Python launch configurations use?

    debugpy is the current adapter type for Python (the older type was python).
  3. 3. Why use --no-reload when debugging Flask or Django servers?

    Auto-reload spawns a child process, so debugging usually works best with it turned off.
  4. 4. How do you make Python debugging use the right libraries?

    The selected interpreter decides which packages are visible when the program runs.

Summary

  • Node.js: F5 on a file, the JavaScript Debug Terminal, Auto Attach, or a node configuration.
  • Python: install the Python extension, select the interpreter, use debugpy configurations.
  • Debug tests from the Testing view; run test runners in a single process for Node.
  • Use skipFiles or justMyCode to avoid wandering into libraries.
  • Most trouble comes from wrong interpreters, missing source maps, reloaders and console settings.

Next: Installing and Managing Extensions