Debugging is an essential skill for any Node.js developer. Whether you're dealing with crashes, memory leaks, or unexpected behavior, knowing the right tools and techniques can save you hours of frustration. In this guide, we'll explore how to debug Node.js applications like a pro—from built-in debugging tools to advanced performance monitoring.
- Using console.log() (But Smarter!)
❌ The Mistake:
Overusing console.log() everywhere without structure.
console.log("User data:", user);
console.log("Order ID:", orderId);
console.log("Something went wrong!");
✅ The Solution:
Use console.table() for better data visualization.
Use console.group() to group logs.
Use log levels (console.warn, console.error) for clarity.
console.table(users);
console.group("Order Details");
console.log("Order ID:", orderId);
console.log("Customer:", customerName);
console.groupEnd();
console.error("Something went wrong!");
- The Built-in Node.js Debugger (node inspect)
Node.js comes with a built-in debugger that lets you step through your code.
How to Use:
- Run your script with node inspect:
node inspect app.js
- Add breakpoints using debugger; in your code:
function fetchData() {
debugger; // Code will pause here
return "Data fetched!";
}
fetchData();
- Open Chrome DevTools by typing:
chrome://inspect
Then click "Open dedicated DevTools for Node".
- Debug with ndb (Google’s Enhanced Debugger)
ndb is an easier alternative to node inspect, providing a visual debugger.
Install & Use:
- Install globally:
npm install -g ndb
- Run your script with:
ndb app.js
- It opens Chrome DevTools automatically!
- Use VS Code’s Built-in Debugger
Visual Studio Code has one of the best debugging tools for Node.js.
How to Set It Up:
Open VS Code, go to the Run & Debug panel.
Click "create a launch.json file" and select Node.js.
Add this configuration:
{
"type": "node",
"request": "launch",
"name": "Debug App",
"program": "${workspaceFolder}/app.js"
}
Set breakpoints in VS Code by clicking on the line number.
Press F5 to start debugging!
Monitor Performance with node --prof
To analyze slow code or memory leaks, use Node.js profiling:
Run Performance Profiling:
node --prof app.js
This generates a v8.log file, which you can analyze using:
node --prof-process v8.log
- Debug Asynchronous Code with async_hooks
Debugging async functions can be tricky. Use async_hooks to track execution.
Example:
const asyncHooks = require("async_hooks");
asyncHooks.createHook({
init(asyncId, type, triggerAsyncId) {
console.log(`Async operation started: ${type} (ID: ${asyncId})`);
},
}).enable();
This helps track callbacks, promises, and async/await operations.
- Debug HTTP Requests with morgan & debug
For debugging API requests, use morgan and debug.
Install them:
npm install morgan debug
Use in Express Apps:
const express = require("express");
const morgan = require("morgan");
const debug = require("debug")("app");
const app = express();
app.use(morgan("dev"));
app.get("/", (req, res) => {
debug("Homepage accessed");
res.send("Hello, world!");
});
app.listen(3000, () => console.log("Server running"));
Now, run your app with:
DEBUG=app node app.js
This enables custom debug logs.
Final Thoughts
Debugging Node.js efficiently saves time, reduces frustration, and improves performance. Start with structured logging (console.log, console.table), use built-in debugging tools (node inspect, ndb), and leverage VS Code’s debugger for an even smoother experience.
Master these techniques, and you’ll debug like a pro!
Top comments (0)