bugl
bugl
HomeLearnPatternsSearch
HomeLearnPatternsSearch

Loading lesson path

Learn/Node.js/Testing & Debugging
Node.js•Testing & Debugging

Node.js Advanced Debugging

Concept visual

Node.js Advanced Debugging

Graph traversalgraph
ABCDE
current
queued
1
4

Start from A

Introduction to Advanced Debugging

Effective debugging is a critical skill for Node.js developers.

While console.log()

is useful for basic debugging, advanced techniques allow you to diagnose complex issues like memory leaks, performance bottlenecks, and race conditions. This tutorial covers advanced debugging techniques and tools to help you solve challenging problems in your Node.js applications. Advanced debugging tools provide capabilities like:

Setting breakpoints and stepping through code execution

Inspecting variable values at runtime

Visualizing memory consumption and finding leaks

Profiling CPU usage to identify performance bottlenecks

Analyzing asynchronous call stacks

Debugging with Chrome DevTools

Node.js includes built-in support for the Chrome DevTools debugging protocol, allowing you to use the powerful Chrome DevTools interface to debug your Node.js applications. Starting Node.js in Debug Mode There are several ways to start your application in debug mode: Standard Debug Mode node --inspect app.js This starts your app normally but enables the inspector on port 9229.

Formula

Break on Start node -- inspect - brk app.js

This pauses execution at the first line of code, allowing you to set up breakpoints before execution begins.

Formula

Custom Port node -- inspect = 127.0.0.1:9222 app.js

This uses a custom port for the inspector.

Connecting to the Debugger

After starting your Node.js application with the inspect flag, you can connect to it in several ways:

Chrome DevTools:

Open Chrome and navigate to chrome://inspect. You should see your Node.js application listed under "Remote Target." Click "inspect" to open DevTools connected to your application:

DevTools URL:

Open the URL shown in the terminal

Formula

(usually something like devtools://devtools/bundled/js_app.html?experiments = true&v8only = true&ws = 127.0.0.1:9229/...

).

Using DevTools for Debugging

Once connected, you can use the full power of Chrome DevTools:

Sources Panel:

Set breakpoints, step through code, and watch variables

Call Stack:

View the current execution stack, including async call chains

Scope Variables:

Inspect local and global variables at each breakpoint

Console:

Evaluate expressions in the current context

Memory Panel:

Take heap snapshots and analyze memory usage

Pro Tip:

Use the Sources panel's "Pause on caught exceptions" feature (the pause button with curved lines) to automatically break when an error occurs.

Debugging in VS Code

Formula

Visual Studio Code provides excellent built - in debugging capabilities for Node.js applications.

Setting Up Node.js Debugging in VS Code You can start debugging your Node.js application in VS Code in several ways: launch.json Configuration:

Formula

Create a.vscode/launch.json file to define how VS Code should launch or attach to your application.

Auto-Attach:

Formula

Enable auto - attach in VS Code settings to automatically debug any Node.js process started with the

--inspect flag.

JavaScript Debug Terminal:

Use the JavaScript Debug Terminal in VS Code to automatically debug any Node.js process started from that terminal.

Example launch.json Configuration

{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/app.js",
"skipFiles": ["<node_internals>/**"]
},
{
"type": "node",
"request": "attach",
"name": "Attach to Process",
"port": 9229
}

]

}

VS Code Debugging Features

VS Code provides powerful debugging capabilities:

Next

Node.js Testing