Debugging Basics
8 examples to get you started debugging Node.js backends - 6 basic and 2 intermediate.
Prerequisites
Node.js 24.18.0 installed locally. Chrome or Edge browser for DevTools.
node --version # v24.18.0Basic Examples
1. Start Inspector on Boot
node --inspect src/main.ts
# Debugger listening on ws://127.0.0.1:9229- Open
chrome://inspect-> Configure -> addlocalhost:9229-> inspect - Default port 9229; use
--inspect=9230if occupied - Bind
0.0.0.0only in trusted dev networks:--inspect=0.0.0.0
Related: Memory Leak Hunt - heap snapshots in DevTools
2. Break on First Line
node --inspect-brk src/main.ts- Process pauses before user code runs - attach debugger then resume
- Useful when bug happens at module load time
- CI should not use
--inspect-brkwithout timeout
3. debugger Statement
export function charge(amountCents: number) {
if (amountCents < 0) {
debugger; // execution pauses when inspector attached
throw new Error("invalid amount");
}
return { charged: amountCents };
}- Remove
debuggerbefore merge - ESLintno-debuggerin CI - Works with
tsxwhen inspector enabled - Prefer breakpoints in DevTools for shared code paths
4. NODE_OPTIONS in npm Scripts
{
"scripts": {
"dev:debug": "NODE_OPTIONS='--inspect' tsx watch src/main.ts"
}
}- Windows: use
cross-env NODE_OPTIONS=--inspect tsx watchrestarts process - reattach DevTools after reload- Pair with VS Code launch.json for team standard
5. Log vs Debugger
import { inspect } from "node:util";
console.log(inspect({ orderId: "ord_1", meta: { retry: 2 } }, { depth: 5, colors: true }));console.logfor production-safe correlation withrequestId- Debugger for stepping when state is unclear
util.inspectprints nested objects readable in terminal
Related: Logging Basics - structured logs in prod
6. Print Stack Trace
function fail() {
throw new Error("payment provider timeout");
}
try {
fail();
} catch (err) {
console.error(err instanceof Error ? err.stack : err);
}Error.captureStackTracecustomizes stack in library code- Log stacks server-side only - never return to API clients in prod
Intermediate Examples
7. Source Maps With TypeScript
// tsconfig.json
{
"compilerOptions": {
"sourceMap": true,
"inlineSources": true
}
}- DevTools shows
.tslines, not transpiled.js tscoutput +node --inspect dist/main.jsfor prod-like debugtsxhandles TS directly without separate compile step
8. Attach to Running Process
# Find PID
pgrep -fl "node dist/main"
# Attach inspector to live process (brief pause)
kill -USR1 <pid>
# Then node logs: Debugger listening...SIGUSR1enables inspect on running Node (if not started with--inspect)- Use only in staging - attaching in prod requires runbook approval
- Combine with Event-Loop Stall Incident profiling
Stack versions: This page was written for Node.js 24.18.0 (Active LTS), npm 10+, TypeScript 5.6+, Express 5, Fastify 5, and NestJS 11.