Debug a Node.js script with the inspector

Low risk
What do risk levels mean?
Read-only
Inspects state without changing anything.
Low risk
Reversible with a routine follow-up command.
Medium risk
Changes state; undo path documented.
Destructive
Deletes or overwrites; confirmation required.

Starts a script under the V8 inspector, paused on the first line, so breakpoints can be set from Chrome DevTools or an editor before any code runs.

nodedebuginspectordevtools

Parameters

Path to the script to debug.

Commands

Check that the default inspector port 9229 is free

lsof -nP -iTCP:9229 -sTCP:LISTEN

Run the script paused at the first line with the inspector listening

node --inspect-brk "index.js"

Verification

node -e "fetch('http://127.0.0.1:9229/json/list').then(r => r.json()).then(t => console.log(t[0].webSocketDebuggerUrl))"

A ws:// debugger URL is printed; chrome://inspect in Chrome shows the paused target.

Undo

Not undoable

Debugging ends when the process exits (Ctrl+C); nothing persists.

Pitfalls

  • The inspect-port check "failing" with no rows means the port is free — that is the green light.
  • Never expose the inspector beyond localhost (--inspect=0.0.0.0) on untrusted networks; it grants full code execution.
  • Use --inspect (without -brk) to attach to a running server without pausing startup.

Related