Node.js Advanced Debugging

Here is a complete and beginner-friendly guide on Node.js Advanced Debugging, covering professional tools, techniques, and real-world debugging workflows.


πŸš€ Node.js Advanced Debugging

Debugging is a crucial part of backend development. Node.js provides multiple advanced techniques and tools to identify and fix bugs, performance issues, and memory leaks.

This guide covers:

βœ… Debugging with Chrome DevTools
βœ… VS Code Debugging
βœ… Inspecting Variables & Call Stack
βœ… Breakpoints (Manual & Conditional)
βœ… Using console advanced tricks
βœ… Performance profiling
βœ… Memory leak debugging
βœ… Using Node.js built-in debugger
βœ… Debugging async code
βœ… Logging best practices


πŸ” 1. Start Node.js with Debug Mode

Enable debugging:

node --inspect app.js

Enable with break before start:

node --inspect-brk app.js

This opens the debugging interface in DevTools.


🧭 2. Debugging in Chrome DevTools

Open Chrome and go to:

chrome://inspect

Click:

πŸ‘‰ Open dedicated DevTools for Node

You get:

  • Breakpoints

  • Watch variables

  • Scope inspection

  • Call stack visualization

  • Step-in / Step-out / Step-over

  • Execution timeline


πŸ§ͺ 3. Debugging with VS Code (Recommended)

Create .vscode/launch.json



 

Features:

  • Set breakpoints in code

  • Hover variables

  • Inspect call stack

  • Pause on exceptions

  • Debug async calls

  • Restart on file save


🎯 4. Manual Breakpoints

You can force the debugger to pause using:

debugger;

Example:



 


🎚️ 5. Conditional Breakpoints

Useful for breaking only when a condition is true:

In VS Code β†’ Right-click breakpoint β†’ Edit β†’ Enter condition:

i === 50

Or in DevTools.


🧩 6. Advanced console Debugging Techniques

Styled logs:



 

Table output:



 

Trace call stack:



 

Timing:



 


🧡 7. Debugging Asynchronous Code

Async stack traces:

Run Node with:

node --trace-warnings myapp.js

To debug promises:



 


🚦 8. Debugging Event Loop Lag

Check event loop delay:


 


⚑ 9. Performance Profiling (CPU Profiling)

Run CPU profiler:

node --prof app.js

Generate readable report:

node --prof-process isolate*.log > report.txt

Shows slow functions, blocking operations, etc.


🧠 10. Memory Leak Debugging

  1. Start Node with heap snapshots:

node --inspect app.js
  1. In Chrome DevTools β†’ Memory:

    • Take heap snapshots

    • Compare memory before & after operations

    • Find detached DOM nodes (if using SSR)

    • Find growing objects

  2. Track heap usage:



 


🧽 11. Using Node’s Built-in Debugger (CLI)

Run:

node inspect app.js

Commands:

Command Description
cont Continue execution
next Step over
step Step into
out Step out
watch('x') Watch variable
repl Open REPL at current breakpoint

πŸ›‘ 12. Debugging Crash Issues

Catch sync errors:



 

Catch async errors:



 


πŸ“ 13. Logging Best Practices

Use logging libraries instead of console.log():

Winston:

npm install winston

Example:


 


🌐 14. Debugging Production Apps

Use monitoring tools:

  • PM2 Monitoring

  • Sentry

  • Datadog

  • New Relic

  • LogDNA

Example PM2 command:

pm2 monit

🎁 Summary

Feature Tools
Basic Debugging --inspect, DevTools
Breakpoints VS Code, Chrome
Async Debugging Call stack tracing
Performance Audit Node Profiler
Memory Leak Detection Heap snapshots
Error Handling process.on()
Logging Winston / Morgan

You may also like...