Debugging with the inspector

Beginner · 15 min

Attaching a debugger to Node with --inspect and --inspect-brk, using Chrome DevTools and VS Code, setting breakpoints and logpoints, and keeping the port safe.

Before thisRunning code, the REPL and the CLIBlocking vs non-blocking I/O

BMR EDUCATION · 15 min

Debugging with the inspector

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by BMR EDUCATIONReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Node embeds V8's inspector. `node --inspect app.js` starts it on `127.0.0.1:9229`, prints a `ws://` URL, and any compatible debugger can attach to it.
  • `--inspect-brk` pauses on the first line so you can set breakpoints before the code you care about has run. Use it for startup bugs and short scripts.
  • Chrome DevTools attaches through `chrome://inspect` or the Node icon in a DevTools window. VS Code can launch the program under a debugger with a `launch.json` entry, or attach to a process already running with `--inspect`.
  • Breakpoints stop execution and show the call stack, scope variables and the current line. Conditional breakpoints stop only when an expression is true, and logpoints print a message without editing code or pausing.
  • Stepping has three moves: step over runs the current line, step into enters a function call, and step out finishes the current function. For async code, the async call stack shows how you got into a callback.
  • `kill -USR1 <pid>` starts the inspector in an already running process on Unix, which is useful on a misbehaving server.
  • The inspector gives full code execution to whoever connects. Keep it bound to localhost, tunnel over SSH if you need remote access, and never expose port 9229 to a network.

Why console.log runs out

Print debugging is fine until it is not. You add a log, rerun, read it, add another, rerun, and each cycle takes a minute and clutters the code. A bug that depends on state three function calls up, or on the 7,000th loop iteration, or on the order async callbacks ran in, is miserable to chase this way.

A debugger lets you stop the program while it is running and look around: the current line, the call stack, every variable in scope. You can poke at values, step forward one line at a time, and keep going. Node gives you this without any installation, because V8 has an inspector built in, the same one Chrome’s DevTools use.

Turning the inspector on

node --inspect app.js
# Debugger listening on ws://127.0.0.1:9229/<uuid>
# For help, see: https://nodejs.org/learn/getting-started/debugging

The flag starts a WebSocket server on 127.0.0.1:9229. Node prints its URL, and any debugger that speaks the Chrome DevTools protocol can attach: Chrome, Edge, VS Code, JetBrains IDEs. I confirmed the process also lists itself at http://127.0.0.1:9333/json/list when started with --inspect-brk=9333, with the file path as its title. That list is how the tools discover it.

A program that finishes in 50 milliseconds is gone before you can attach. The fix is --inspect-brk:

node --inspect-brk app.js

It starts the inspector and pauses on the very first line, waiting for you. You attach, set breakpoints where you want them, and press resume. Use --inspect-brk for scripts and startup bugs, and --inspect for a long-running server that you attach to when you need it.

If a server is already running, you can switch the inspector on without restarting it. On macOS and Linux:

kill -USR1 <pid>

I tried it on a running Node process, and it printed the same Debugger listening line to its stderr. That is handy for a stuck process.

Attaching from Chrome DevTools

Open chrome://inspect in Chrome. A process started with --inspect appears under Remote Target, and clicking “inspect” opens a DevTools window wired to it. The same page has an “Open dedicated DevTools for Node” link that attaches to Node processes automatically. You get the Sources panel, with your files, a call stack and scopes, and the Console panel, which evaluates expressions in the paused frame.

Attaching from VS Code

VS Code has the debugger built in. The two ways to use it:

  • Launch: VS Code starts your program under the debugger. Create .vscode/launch.json:

    {
      "version": "0.2.0",
      "configurations": [
        { "type": "node", "request": "launch", "name": "Run app", "program": "${workspaceFolder}/app.js" }
      ]
    }

    Press F5. Breakpoints set in the gutter are hit.

  • Attach: start the program yourself with --inspect, then use a config with "request": "attach" and "port": 9229 to connect to it. This suits servers started by other tools.

I could not drive VS Code in this environment, so the launch.json above is from the standard documented format and not something I ran. The inspector side of both ways is what I checked above.

Breakpoints, stepping and logpoints

A breakpoint is a line where execution pauses. Click the gutter, or write debugger; in the code. A debugger statement does nothing unless a debugger is attached, and does stop there when one is, so remember to remove it before you commit. In my test file:

function add(a, b) {
  const sum = a + b;
  debugger;          // pauses here when the inspector is attached
  return sum;
}

Once paused, you see the call stack, the variables in each scope, and a Watch panel for expressions. You can move forward in three ways:

  • Step over runs the current line and stops at the next one.
  • Step into goes inside the function being called on this line.
  • Step out runs until the current function returns.
  • Resume continues to the next breakpoint.

Two refinements save the most time. A conditional breakpoint only pauses when an expression is true, such as i === 7342 in a long loop. A logpoint prints a message, with expressions in braces, without stopping or changing the file, so it replaces a throwaway console.log with no code edit to undo.

For async code, the debugger shows an async call stack that stitches together the awaiting chain across ticks, so you can see which request handler led into a callback. If you are unsure why things ran in the order they did, the event loop phases is the background. If you wonder why a pause freezes the whole server, remember the process is single-threaded, as in blocking vs non-blocking I/O: while paused, nothing else runs.

Debugging tests and TypeScript

You can debug a test the same way, since a test is just a Node process: node --inspect-brk --test file.test.js. Test runners such as Vitest and Jest have documented recipes for the same thing. For TypeScript running in Node, source maps let the debugger show your original .ts lines, as described in running TypeScript without a build step.

Keep the port closed

The inspector protocol lets a connecting client evaluate any JavaScript in your process. That means reading environment variables and secrets, calling any function, and opening network connections. It is full remote code execution for whoever can reach the port. So:

  • It binds to 127.0.0.1 by default. Do not change that to 0.0.0.0 on a server.
  • For a remote machine, tunnel with SSH, for example ssh -L 9229:127.0.0.1:9229 host, rather than opening the port.
  • Do not leave --inspect in production start scripts. Use SIGUSR1 when you must, and restart afterwards.

This links to the hardening ideas in the permission model and hardening. A debugger is a deliberate backdoor, so treat it like one.

What the video covers

BMR Education’s fifteen-minute video, from January 2026, describes itself as covering why console.log is not enough, the Node inspector fundamentals, Chrome DevTools step-through, the VS Code debugger with launch configs and attach mode, advanced breakpoints (conditional, logpoints, hit counts), step controls, async debugging, inspecting variables, scopes and call stacks, performance debugging with the event loop, and best practices. That is the lesson’s scope almost point for point. It has no chapters and its transcript was not available, so this list is taken from its description rather than from what is said. It is a small channel of about 3,000 subscribers, and the original audio language could not be confirmed from an audio-track list, though its captions are English and the channel publishes in English. The inspector commands, the SIGUSR1 behaviour and the safety guidance are carried by this prose, and I ran what I could on Node 26.

Where this fits

Debugging finds a bug once you suspect where it is. For problems that show up only in production, such as slow requests and growing memory, you need to measure instead, starting with logging in Node with pino.

Check yourself

  1. 01When do you use `--inspect-brk` instead of `--inspect`?

    When the bug happens before you could attach, such as during startup or in a short script that exits quickly. `--inspect-brk` pauses before the first line, so the debugger attaches, you set breakpoints, and then you resume. With plain `--inspect`, the program runs straight away and may finish before you attach.

  2. 02A loop runs 10,000 times and the bug only shows on the 7,342nd iteration. How do you stop there without clicking resume thousands of times?

    Right-click the line and add a conditional breakpoint with an expression such as `i === 7342`, so execution only pauses when it is true. A hit-count breakpoint does the same by number of passes. A logpoint prints values to the console at that line, which can narrow things down without pausing at all.

  3. 03Why is it dangerous to start a production server with `node --inspect=0.0.0.0:9229`?

    The inspector protocol lets any client evaluate arbitrary JavaScript in your process, with access to its environment variables, secrets and network. Binding to all interfaces exposes that to anyone who can reach the port. Keep it on `127.0.0.1` and reach it through an SSH tunnel, or enable it temporarily with `SIGUSR1` and close it afterwards.

  4. 04Your server is already running and is behaving oddly. How can you attach a debugger without restarting it?

    On Linux and macOS send `SIGUSR1` with `kill -USR1 <pid>`. Node starts the inspector on the default port and prints the `ws://` URL, and you can then attach from DevTools or VS Code. The signal handler can be turned off with `--disable-sigusr1`.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark