Inspector in Node.js: Debugging and Profiling with the DevTools Protocol

The node:inspector module provides an API for interacting with the V8 Inspector. It allows developers to connect to the Chrome DevTools Protocol, enabling debugging, CPU profiling, heap profiling, and network inspection. The Inspector module offers both a Promises-based API and a Callback-based API, making it flexible for different coding styles.

inspector.Session / connect / disconnect / postCPU Profiler / Heap Profilerinspector.open / inspector.url / inspector.waitForDebugger

~2 min read · Updated Dec 29, 2025

1. Introduction


The inspector module can be accessed via require('node:inspector') or require('node:inspector/promises'). It enables communication with the V8 Inspector back-end and supports Chrome DevTools Protocol domains for runtime inspection and event listening.


2. Class: inspector.Session


  • new inspector.Session(): Creates a new session instance.
  • session.connect(): Connects to the inspector back-end.
  • session.connectToMainThread(): Connects to the main thread inspector (for worker threads).
  • session.disconnect(): Closes the session and clears inspector state.
  • session.post(method[, params]): Sends commands to the inspector back-end (e.g., Runtime.evaluate).

3. Events


  • inspectorNotification: Fired when any notification is received from the V8 Inspector.
  • Specific events such as Debugger.paused can be listened to for breakpoints and execution suspension.

4. Profiling


  • CPU Profiler: Enable with Profiler.enable, start with Profiler.start, and stop with Profiler.stop to capture CPU usage data.
  • Heap Profiler: Use HeapProfiler.takeHeapSnapshot to capture memory usage and save snapshots for analysis.

5. Common Inspector Methods


  • inspector.open([port, host, wait]): Activates the inspector on a given port and host.
  • inspector.url(): Returns the active inspector’s URL.
  • inspector.waitForDebugger(): Blocks execution until a debugger client connects.
  • inspector.close(): Closes all connections and deactivates the inspector.

6. DevTools Integration


The Inspector can broadcast Chrome DevTools Protocol events such as Network.requestWillBeSent, Network.responseReceived, and Network.webSocketCreated. These events allow developers to monitor HTTP requests, responses, and WebSocket activity in real time.


7. Example


const inspector = require('node:inspector');
const session = new inspector.Session();
session.connect();

session.post('Runtime.evaluate', { expression: '2 + 2' }, 
             (err, { result }) => console.log(result));
// Output: { type: 'number', value: 4, description: '4' }

Conclusion


The inspector module in Node.js is a powerful tool for debugging and profiling. By connecting to the Chrome DevTools Protocol, developers can monitor runtime behavior, capture CPU and memory profiles, and inspect network activity, gaining deep insights into application performance and execution.


Written & researched by Dr. Shahin Siami

Related Articles

Comprehensive Guide to the Node.js VM Module (node:vm)

The node:vm module allows you to compile and execute JavaScript code inside isolated V8 contexts — essentially creating lightweight sandboxes within your Node.js application. These contexts have their own global scope and can run code independently from the main environment. However, vm is NOT a security sandbox. It is powerful for dynamic code execution, template engines, REPLs, plugin systems, and controlled module execution, but it must never be used to run untrusted code.

Continue

Comprehensive Guide to the V8 Module in Node.js (node:v8)

The node:v8 module exposes low-level APIs that interact directly with the V8 JavaScript engine embedded in Node.js. . These APIs provide access to heap statistics, heap snapshots, coverage tools, serialization mechanisms, V8 flags, object queries, and promise lifecycle hooks. The module is essential for performance analysis, memory debugging, tooling, and advanced Node.js internals work.

Continue

Comprehensive Guide to Node.js Worker Threads (node:worker_threads)

The node:worker_threads module enables true multithreading in Node.js by running JavaScript in separate threads. While Node.js is traditionally single‑threaded, worker threads allow CPU‑intensive tasks to run in parallel without blocking the event loop. They support shared memory, zero‑copy transfers, worker pools, resource limits, and advanced synchronization APIs. Worker threads are ideal for heavy computation, data processing, and parallel workloads—while async I/O remains best handled by the main thread.

Continue

Comprehensive Guide to the Node.js util Module (node:util)

The node:util module provides a powerful collection of helper functions used throughout Node.js core and extremely useful for application developers. These utilities support debugging, inspection, formatting, type checking, callback/Promise conversions, argument parsing, text encoding, MIME handling, and more. It is one of the most versatile and essential toolkits in the Node.js ecosystem.

Continue

Comprehensive Guide to the URL Module in Node.js

The node:url module provides tools for parsing, constructing, and manipulating URLs. Node.js supports two URL APIs: WHATWG URL API — modern, browser‑compatible, standards‑based. Legacy Node.js URL API — older, Node‑specific, now discouraged. The WHATWG API is the recommended approach for all modern applications. It provides a clean, consistent interface for working with URL components, query parameters, and structured URL patterns.

Continue

راهنمای جامع UDP / Datagram Sockets در Node.js

ماژول node:dgram پیاده‌سازی کامل سوکت‌های UDP را در Node.js فراهم می‌کند. UDP یک پروتکل سبک، بدون اتصال (connectionless) و مناسب برای برنامه‌های بلادرنگ مانند VoIP، بازی‌ها، IoT، سیستم‌های پخش (broadcast) و چندپخشی (multicast) است. این ماژول امکان ساخت سوکت، ارسال و دریافت دیتاگرام، مدیریت TTL، عضویت در گروه‌های multicast، کنترل بافرها، و مدیریت رفتار سطح پایین شبکه را فراهم می‌کند.

Continue