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:

CommandDescription
contContinue execution
nextStep over
stepStep into
outStep out
watch('x')Watch variable
replOpen 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

FeatureTools
Basic Debugging--inspect, DevTools
BreakpointsVS Code, Chrome
Async DebuggingCall stack tracing
Performance AuditNode Profiler
Memory Leak DetectionHeap snapshots
Error Handlingprocess.on()
LoggingWinston / Morgan

You may also like...