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:
Enable with break before start:
This opens the debugging interface in DevTools.
π§ 2. Debugging in Chrome DevTools
Open Chrome and go to:
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:
Example:
ποΈ 5. Conditional Breakpoints
Useful for breaking only when a condition is true:
In VS Code β Right-click breakpoint β Edit β Enter condition:
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:
To debug promises:
π¦ 8. Debugging Event Loop Lag
Check event loop delay:
β‘ 9. Performance Profiling (CPU Profiling)
Run CPU profiler:
Generate readable report:
Shows slow functions, blocking operations, etc.
π§ 10. Memory Leak Debugging
-
Start Node with heap snapshots:
-
In Chrome DevTools β Memory:
-
Take heap snapshots
-
Compare memory before & after operations
-
Find detached DOM nodes (if using SSR)
-
Find growing objects
-
-
Track heap usage:
π§½ 11. Using Nodeβs Built-in Debugger (CLI)
Run:
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:
Example:
π 14. Debugging Production Apps
Use monitoring tools:
-
PM2 Monitoring
-
Sentry
-
Datadog
-
New Relic
-
LogDNA
Example PM2 command:
π 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 |
