Debugging Basics
8 examples to get you started debugging Node.js backends - 6 basic and 2 intermediate.
Search across all documentation pages
8 examples to get you started debugging Node.js backends - 6 basic and 2 intermediate.
Node.js 24.18.0 installed locally. Chrome or Edge browser for DevTools.
node --version # v24.18.0node --inspect src/main.ts
# Debugger listening on ws://127.0.0.1:9229chrome://inspect -> Configure -> add localhost:9229 -> inspect--inspect=9230 if occupied0.0.0.0 only in trusted dev networks: --inspect=0.0.0.0Related: Memory Leak Hunt - heap snapshots in DevTools
node --inspect-brk src/main.ts--inspect-brk without timeoutdebugger Statementexport function charge(amountCents: number) {
if (amountCents < 0) {
debugger; // execution pauses when inspector attached
throw new Error("invalid amount");
}
return { charged: amountCents };
}debugger before merge - ESLint no-debugger in CItsx when inspector enabledNODE_OPTIONS in npm Scripts{
"scripts": {
"dev:debug": "NODE_OPTIONS='--inspect' tsx watch src/main.ts"
}
}cross-env NODE_OPTIONS=--inspecttsx watch restarts process - reattach DevTools after reloadimport { inspect } from "node:util";
console.log(inspect({ orderId: "ord_1", meta: { retry: 2 } }, { depth: 5, colors: true }));console.log for production-safe correlation with requestIdutil.inspect prints nested objects readable in terminalRelated: Logging Basics - structured logs in prod
function fail() {
throw new Error("payment provider timeout");
}
try {
fail();
} catch (err) {
console.error(err instanceof Error ? err.stack : err);
}Error.captureStackTrace customizes stack in library code// tsconfig.json
{
"compilerOptions": {
"sourceMap": true,
"inlineSources": true
}
}.ts lines, not transpiled .jstsc output + node --inspect dist/main.js for prod-like debugtsx handles TS directly without separate compile step# Find PID
pgrep -fl "node dist/main"
# Attach inspector to live process (brief pause)
kill -USR1 <pid>
# Then node logs: Debugger listening...SIGUSR1 enables inspect on running Node (if not started with --inspect)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.
Reviewed by Chris St. John·Last updated Jul 16, 2026