Node.js debugging tools
Debuggers, logs, request dashboards, profilers and error trackers: the tools for debugging a Node.js app, and when each one helps.
npm install peekinsideDebugging a Node.js app takes a few kinds of tools, because bugs show up in different places: a wrong value you can reproduce, a request that failed an hour ago, a page that got slow, a process that slowly eats memory. Here is what each kind of tool is good at, starting with what ships with Node.js itself.
| You want to... | Reach for |
|---|---|
| Step through code you can reproduce | A debugger: node --inspect with Chrome DevTools or VS Code |
| See what a request did after it happened | A request dashboard such as PeekInside |
| Find slow requests and N+1 queries | A dashboard that records queries per request |
| Find what uses the CPU or leaks memory | A profiler: --cpu-prof, heap snapshots |
| Know about errors in production | An error tracker or monitoring service |
Debuggers: pause and step through#
Node.js has a debugger built in. Run your app with --inspect, and any client that speaks the inspector protocol can attach, set breakpoints, step through code and look at every variable in scope:
node --inspect server.js # listen for a debugger, on 127.0.0.1:9229
node --inspect-brk server.js # the same, paused on the first line
node inspect server.js # Node's own command-line debugger
- Chrome DevTools: open
chrome://inspectand click your process. You get the Sources panel, the console, and the Memory and Performance panels for the same process. - VS Code: open a JavaScript Debug Terminal and run your app as usual (
npm run devworks); it attaches by itself. Breakpoints are set in your editor, in TypeScript too. - WebStorm and other JetBrains IDEs have the same with a Node.js run configuration.
A debugger is the right tool when you can reproduce the bug and want to see the state at one line. It stops the whole process while paused, so it's for your machine, not for a server handling real traffic.
Logs: the trail you leave yourself#
console.log is the first debugging tool for most of us, and structured loggers (pino, winston) make the trail searchable. Node's core modules have logs of their own: NODE_DEBUG=http,net node server.js prints what the HTTP and socket layers do, and libraries built on the debug package, Express among them, print theirs with DEBUG=express:*.
Logs answer "what happened" only where you thought to log. And in a busy app, lines from different requests interleave, so the hard part is knowing which lines belong to the request that failed.
Request dashboards: what each request did#
This is the gap PeekInside fills: instead of logging by hand, it records every request your app handles, with everything that happened while it was handled.
import PeekInside from 'peekinside';
PeekInside.attach(app); // Express, Koa, Fastify, NestJS, Hono or an http.Server
For each request, the dashboard on your machine shows:
- the request and the response: headers, bodies, status, duration;
- the exception it threw, with your source lines around the line that threw, TypeScript included;
- every database query it ran (pg, mysql2, Prisma, MongoDB, Mongoose, and the ORMs on top of them), with slow queries and N+1 loops flagged;
- the outgoing calls it made, through
node:http, axios and the rest, orfetch; - the log lines written while it was handled, from the console, pino, winston or NestJS's Logger;
- all of it on a timeline, in order, so you see where the time went.
You can look at any request after the fact: the one that failed while you were testing something else, or the one a teammate sent you the id of. Once you've fixed the bug, Replay sends that request to your app again and opens the new one next to it, so you see the fix work without rebuilding the request in curl. PeekInside.dump(value) puts a value in the dashboard under its request, as a tree you can open, instead of in your terminal. It's free, needs no account, and keeps everything on your machine.
If you came from Laravel, this is what Telescope does there.
Profilers: CPU and memory#
When the problem is "slow" or "growing", profiles tell you where:
node --cpu-prof server.js # writes a .cpuprofile when the process exits
node --heapsnapshot-signal=SIGUSR2 server.js # kill -USR2 <pid> writes a heap snapshot
Open either in Chrome DevTools: .cpuprofile files in the Performance panel, .heapsnapshot files in the Memory panel, where comparing two snapshots shows what kept growing between them. node --inspect gives you the same panels live.
To see whether a leak or a slow patch is happening at all, and when, PeekInside's Runtime page charts memory, CPU and event-loop delay over time, and records event-loop blocks on the requests that were in flight when they happened.
Error trackers and monitoring: production#
On a server, nobody is there to watch a dashboard or attach a debugger. Error trackers and monitoring services collect errors and performance from production, and alert you. There are many: Sentry, Datadog, New Relic, AppSignal and others, each with its own focus and pricing.
PeekInside Cloud is the same dashboard as your local one, for your servers: the same SDK sends to it when PEEKINSIDE_API_KEY is set. Every error comes with its request, queries and logs, and alerts reach you by email or Slack. It's encrypted end to end: your servers encrypt what they send, your browser decrypts it, and PeekInside Cloud only ever stores ciphertext.
Putting them together#
A typical path through a bug:
- Something fails. The request dashboard (or, in production, an alert) shows the request, the error and the line of code.
- The cause isn't obvious. The request's queries, calls and log lines show what led up to it.
- You need to see the state at one line. Reproduce it with the same request, with a debugger attached and a breakpoint on that line.
- It's about performance. The timeline shows which query or call took the time; a CPU profile shows where the rest went.
Questions#
What is the best debugging tool for Node.js?#
There isn't one: a debugger (Chrome DevTools or VS Code with --inspect) for stepping through code you can reproduce, a request dashboard like PeekInside for seeing what each request did, a profiler for CPU and memory, and an error tracker for production. They answer different questions.
How do I debug a Node.js app running in Docker?#
Start Node with --inspect=0.0.0.0:9229, publish the port (-p 9229:9229), and attach Chrome DevTools or VS Code to it. The inspector gives full control of the process, so only expose it where nobody else can reach it.
How do I see the requests my Express app receives?#
Add PeekInside.attach(app): every request appears in the dashboard with its headers, body, response, queries and errors. See Express.
Try it on your app
Install the package, add PeekInside.attach(app), and start your app as usual: it prints the link to its dashboard. Free, no account, and your data stays on your machine. On your servers, the same line reports to PeekInside Cloud once it has an API key.