Back to results for “Performance”
Node.js › Advanced Node.js

How do you use the --inspect flag and Chrome DevTools to profile a running Node.js application in production?

Advanced

Answer

Running node --inspect (or --inspect-brk to pause on the first line) opens a WebSocket debugging port that Chrome DevTools or VS Code can attach to, exposing the CPU profiler, heap snapshots, and live breakpoints against the running process. In production, --inspect should be bound to localhost only (never a public interface) and typically accessed through an SSH tunnel, since anyone who can connect to the inspector port gets full remote code execution in that process.
node --inspect=127.0.0.1:9229 server.js
// then open chrome://inspect in Chrome, or connect VS Code's Node debugger
// SSH tunnel from a local machine to a remote server:
// ssh -L 9229:localhost:9229 user@remote-host
Real-world example An intermittent memory leak in a production API is diagnosed by attaching Chrome DevTools via an SSH-tunneled --inspect session, taking two heap snapshots ten minutes apart, and comparing retained object counts to identify a growing array of unclosed database connections.

Common follow-ups: Why is exposing the inspector port publicly considered a critical security vulnerability?;What's the difference between --inspect and --inspect-brk in a debugging workflow?