DEV Community

Diego Liascovich
Diego Liascovich

Posted on

🛠️ Advanced Node.js Debugging: Beyond console.log

By Diego Liascovich

Full-Stack Developer | Microservices | Angular | Node.js


In this post, we’ll explore powerful, professional ways to debug Node.js applications, step by step — and all without relying on console.log.


🧠 Why You Should Ditch console.log

  • Clutters code
  • Slows performance
  • Doesn't pause execution
  • Not useful for complex stack traces or state inspection

🔍 1. The debugger Statement

Place debugger; in your Node.js code:

function calculateTotal(items) {
  const total = items.reduce((sum, item) => sum + item.price, 0);
  debugger; // Execution will pause here
  return total;
}
Enter fullscreen mode Exit fullscreen mode

Then run your app with:

node inspect index.js
Enter fullscreen mode Exit fullscreen mode

This opens the built-in debugger in the terminal.


🧪 2. Chrome DevTools + --inspect

Use the V8 inspector in Chrome to debug with a GUI:

Step 1: Add debugger to your code

debugger;
Enter fullscreen mode Exit fullscreen mode

Step 2: Run with --inspect

node --inspect index.js
Enter fullscreen mode Exit fullscreen mode

Step 3: Open Chrome and go to:

chrome://inspect
Enter fullscreen mode Exit fullscreen mode

Click “Open dedicated DevTools for Node” and you’ll see a full debugger just like front-end JS debugging.


🖥️ 3. Debugging with VSCode (Recommended)

VSCode provides the most comfortable experience.

Step 1: Add .vscode/launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Debug My App",
      "program": "${workspaceFolder}/index.js"
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

Step 2: Add breakpoints

Click in the gutter of VSCode next to the lines you want to inspect.

Step 3: Press F5 to start debugging

You’ll get:

  • Call stack
  • Watches
  • Scope
  • Live variable inspection

🧭 4. Remote Debugging

Useful when debugging apps running inside Docker or in the cloud.

Run Node.js with:

node --inspect=0.0.0.0:9229 index.js
Enter fullscreen mode Exit fullscreen mode

Then from your local machine:

  • Open chrome://inspect
  • Add the remote target (IP:9229)

Make sure the port is exposed and secured! Never expose --inspect in production without IP restriction.


🔁 5. Using ndb: GUI Debugging Made Easy

ndb is a standalone debugger from the Chrome team.

npm install -g ndb
ndb index.js
Enter fullscreen mode Exit fullscreen mode

You’ll get:

  • Source-mapped debugging
  • Breakpoints
  • Heap snapshots
  • Console context

🧰 6. Smart Logging with debug

Instead of polluting your app with console.log, use the debug library:

npm install debug
Enter fullscreen mode Exit fullscreen mode
const debug = require('debug')('app:init');
debug('Initializing app...');
Enter fullscreen mode Exit fullscreen mode

Enable output with:

DEBUG=app:* node index.js
Enter fullscreen mode Exit fullscreen mode

You can turn logs on/off per namespace (app:*, db:*, etc.).


🧪 7. Debugging Tests

If you’re testing with Jest, Mocha, or Vitest:

node --inspect-brk ./node_modules/.bin/jest tests/example.spec.js
Enter fullscreen mode Exit fullscreen mode

Or with TypeScript:

node --inspect-brk -r ts-node/register src/index.ts
Enter fullscreen mode Exit fullscreen mode

Then attach Chrome or VSCode.


🧼 Summary: Debug Like a Pro

Tool Use Case GUI Breakpoints Live Vars
inspect Built-in debugger 🟡 Console only
DevTools Full browser debugger
VSCode IDE-native debugging
ndb Lightweight GUI
debug Clean runtime logging ✅ (logs)

💡 Pro Tips

  • Use debugger; instead of console.log to stop and inspect.
  • Prefer debug or winston over console.log in production.
  • Always use .vscode/launch.json for repeatable debugging.

Top comments (0)