DEV Community

Cover image for Debugging Node.js Applications Like a Pro: Tools and Techniques
Raji moshood
Raji moshood

Posted on

Debugging Node.js Applications Like a Pro: Tools and Techniques

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.

  1. 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!");
Enter fullscreen mode Exit fullscreen mode

✅ 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!");

Enter fullscreen mode Exit fullscreen mode
  1. 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:

  1. Run your script with node inspect:

node inspect app.js

  1. Add breakpoints using debugger; in your code:
function fetchData() {
  debugger; // Code will pause here
  return "Data fetched!";
}
fetchData();

Enter fullscreen mode Exit fullscreen mode
  1. Open Chrome DevTools by typing:

chrome://inspect

Then click "Open dedicated DevTools for Node".

  1. Debug with ndb (Google’s Enhanced Debugger)

ndb is an easier alternative to node inspect, providing a visual debugger.

Install & Use:

  1. Install globally:
npm install -g ndb

Enter fullscreen mode Exit fullscreen mode
  1. Run your script with:
ndb app.js

Enter fullscreen mode Exit fullscreen mode
  1. It opens Chrome DevTools automatically!

  1. 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:

  1. Open VS Code, go to the Run & Debug panel.

  2. Click "create a launch.json file" and select Node.js.

  3. Add this configuration:

{
  "type": "node",
  "request": "launch",
  "name": "Debug App",
  "program": "${workspaceFolder}/app.js"
}
Enter fullscreen mode Exit fullscreen mode
  1. Set breakpoints in VS Code by clicking on the line number.

  2. Press F5 to start debugging!

  3. Monitor Performance with node --prof

To analyze slow code or memory leaks, use Node.js profiling:

Run Performance Profiling:

node --prof app.js

Enter fullscreen mode Exit fullscreen mode

This generates a v8.log file, which you can analyze using:

node --prof-process v8.log

Enter fullscreen mode Exit fullscreen mode
  1. 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();

Enter fullscreen mode Exit fullscreen mode

This helps track callbacks, promises, and async/await operations.

  1. Debug HTTP Requests with morgan & debug

For debugging API requests, use morgan and debug.

Install them:

npm install morgan debug
Enter fullscreen mode Exit fullscreen mode

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"));
Enter fullscreen mode Exit fullscreen mode

Now, run your app with:

DEBUG=app node app.js
Enter fullscreen mode Exit fullscreen mode

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!

NodeJS #Debugging #WebDevelopment #JavaScript #CodingTips #BackendDevelopment

Top comments (0)