TechBuzzPro

Debug Node.js Apps in VS Code: Step-by-Step

· 5 min read · Apps & Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Debugging Node.js applications in Visual Studio Code lets you pause execution, inspect variables, and step through code line by line—all without leaving your editor. This guide covers the built-in debugger, from a simple launch configuration to advanced features like breakpoints, watch expressions, and attaching to a running process. You'll need VS Code (1.70 or later), Node.js (14+), and a Node.js project to follow along.

What You'll Need

  • Visual Studio Code installed (free from code.visualstudio.com)
  • Node.js and npm installed (check with node -v and npm -v)
  • A Node.js project folder opened in VS Code
  • The JavaScript/TypeScript language extensions (usually built-in)

Step-by-Step

  1. Open your project in VS Code. Use File > Open Folder and select the root of your Node.js application. If you don't have a project, create a simple one: npm init -y and add a file app.js with console.log('hello').
  2. Create a launch configuration. Click the Run and Debug icon in the left sidebar (or press Ctrl+Shift+D). Click the gear icon to create a launch.json file. VS Code will detect Node.js and offer a template—select Node.js.
  3. Understand the generated configuration. The default launch.json looks like this:
    {
      "version": "0.2.0",
      "configurations": [
        {
          "type": "node",
          "request": "launch",
          "name": "Launch Program",
          "program": "${file}"
        }
      ]
    }
    The program field points to the file to run. Change it to your entry point, e.g., "program": "${workspaceFolder}/app.js".
  4. Add breakpoints. Click in the gutter (the column to the left of line numbers) in any JavaScript file to set a red dot. Breakpoints pause execution at that exact line.
  5. Start debugging. Press F5 or click the green play button in the Run and Debug panel. The app will start and stop at your first breakpoint.
  6. Use the debugging controls. When paused, a floating toolbar appears: Continue (F5), Step Over (F10), Step Into (F11), Step Out (Shift+F11), Restart (Ctrl+Shift+F5), and Stop (Shift+F5).
  7. Inspect variables. In the left panel, the Variables section shows local, closure, and global scopes. Hover over any variable in the editor to see its current value.
  8. Add watch expressions. In the Watch section, click the plus icon and type an expression like myArray.length or user.name. It updates as you step.
  9. Use the Debug Console. While paused, type expressions in the Debug Console to evaluate them in the current context. For example, type typeof myVar or call a function.
  10. Handle exceptions. In the breakpoints section (red circle with a pause icon), enable All Exceptions to break on any thrown error, or Uncaught Exceptions to stop only on unhandled ones.
  11. Attach to a running process. If your app is already running (e.g., node --inspect app.js), create a configuration with "request": "attach" and set "port": 9229. Then press F5 to attach.
  12. Debug TypeScript and ES modules. For TypeScript, use "type": "node" and add "preLaunchTask": "tsc: build" to compile first. For ES modules ("type": "module" in package.json), add "runtimeArgs": ["--experimental-vm-modules"] if needed.

Common Mistakes

  • Wrong entry point: The program field points to a file that isn't the main script, so breakpoints in other files never hit.
  • Forgetting to set breakpoints: If you press F5 and the app runs to completion, you likely have no breakpoints or they're in files not loaded.
  • Using console.log as a substitute: It works but doesn't pause execution; you can't inspect state at that moment.
  • Breakpoints in transpiled code: If you're debugging compiled TypeScript or Babel output, source maps must be enabled. Add "sourceMaps": true to your configuration.
  • Attaching to the wrong port: The default inspect port is 9229, but if your app uses another, the attach fails.
  • Not using skipFiles: Stepping into node_modules code is tedious. Add "skipFiles": ["/**", "${workspaceFolder}/node_modules/**"] to your config.

Tips

  • Use conditional breakpoints: Right-click a breakpoint, choose Edit Breakpoint, and add a condition like i === 5 to only pause when true.
  • Log points: Instead of a breakpoint, right-click in the gutter and choose Add Log Point. Type a message like User logged in: {user.name}—it logs without pausing.
  • Debug npm scripts: Add a configuration with "runtimeExecutable": "npm", "runtimeArgs": ["run", "dev"], and "port": 9229 to debug scripts that start a server.
  • Use the Call Stack: When paused, the call stack shows the chain of function calls. Click any frame to see that scope's variables.
  • Persist breakpoints: Breakpoints are saved per workspace, so they survive restarts.
  • Use the integrated terminal: Run node --inspect-brk to pause at the first line, then attach from VS Code.

Troubleshooting

  • Breakpoints not hitting: Check that the file is actually loaded. If it's a module that isn't imported, it never runs. Also verify the program path is correct and that you're not using a compiled version without source maps.
  • "Cannot connect to runtime process" error: This usually happens with attach configurations. Ensure the app is running with --inspect and that the port matches. Try node --inspect=0.0.0.0:9229 if you're in a container.
  • Debugger skips lines: If you're using TypeScript or Babel, enable source maps. Add "sourceMaps": true and ensure your build outputs .map files.
  • Variables not updating: The debugger shows values at the current paused line. Step to the next line to see changes. If a variable is undefined, it may be out of scope.
  • Performance is slow: Stepping through large loops is slow. Set a conditional breakpoint or use Log Points instead.
  • Debugger stops on unhandled exceptions in libraries: Disable All Exceptions and only keep Uncaught Exceptions enabled, or add the library to skipFiles.

Frequently Asked Questions

How do I debug a Node.js script that takes command-line arguments?

Add "args": ["--flag", "value"] to your launch configuration. The arguments are passed to your script when it starts.

Can I debug multiple processes at once?

Yes. Create multiple configurations and use a compound configuration in launch.json with "compounds": [{"name": "Full Stack", "configurations": ["Server", "Client"]}]. Then select that compound and press F5.

How do I debug a Node.js app running in Docker?

Run the container with --inspect=0.0.0.0:9229 and map the port (e.g., -p 9229:9229). Then use an attach configuration with "port": 9229 and "localRoot" and "remoteRoot" set to the workspace and container paths.

Why are my breakpoints grayed out?

Gray breakpoints mean the file isn't loaded or the debugger hasn't attached yet. Start debugging and then open the file. If using a build step, ensure the compiled file is the one being executed.

How do I debug a Node.js script that runs with node --watch?

VS Code doesn't natively support watch mode, but you can use a launch configuration with "runtimeArgs": ["--watch"] and set "restart": true to restart the debugger on file changes.

Can I use the debugger with npm scripts that use environment variables?

Yes. Add "env": {"MY_VAR": "value"} to your configuration. For variables from a .env file, use a tool like dotenv or set them in the terminal before launching.