A real debugger finds bugs faster than scattering console.log calls.
Node includes the V8 inspector, the same debugging protocol Chrome uses. Start your program with a flag:
node --inspect app.js— runs normally and listens for a debugger on127.0.0.1:9229.node --inspect-brk app.js— pauses before the first line, so you can set breakpoints in startup code.node --inspect-wait app.js— waits for a debugger to attach, then runs.
Then attach a client:
- Chrome: open
chrome://inspect, click "Open dedicated DevTools for Node". You get Sources, breakpoints, the console in your program's context, plus Memory and Performance tabs. - VS Code: the JavaScript Debug Terminal auto-attaches to any
nodeyou start in it, or create a launch configuration. Breakpoints are set in the editor gutter.
Inside the debugger you can step over/into/out, inspect the call stack (including async frames, so you see which await led here), add watch expressions, and set conditional breakpoints or logpoints that print without pausing. A debugger; statement in code acts as a breakpoint when a debugger is attached.
Never expose the inspector on a public interface: --inspect=0.0.0.0 gives anyone who can reach the port full code execution in your process. To debug a remote machine, keep the default localhost binding and use an SSH tunnel.
Other helpful tools: node inspect app.js is a terminal debugger; NODE_DEBUG=http,net turns on internal debug logs of core modules; console.trace() prints the current stack; --enable-source-maps makes stack traces point to TypeScript sources. For tests, node --inspect-brk --test lets you step through a failing test.
Example
# Pause at the first line and attach Chrome DevTools (chrome://inspect)
node --inspect-brk server.js
# Debug a single failing test file
node --inspect-brk --test test/user.test.js
# Readable stack traces for transpiled code
node --enable-source-maps dist/server.js
# Internal debug logging from core modules
NODE_DEBUG=http,net node server.js
# Remote host: keep the inspector on localhost and tunnel it
ssh -L 9229:127.0.0.1:9229 user@server # then attach to localhost:9229Remember
--inspect/--inspect-brkenable the debugger on port 9229- Attach via chrome://inspect or VS Code's JavaScript Debug Terminal
- Conditional breakpoints and logpoints avoid edit-restart cycles
- Never bind the inspector to a public interface
Watch out for
- Running
--inspect=0.0.0.0on a server, which allows remote code execution.