• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!DOCTYPE html>
2<html lang="en">
3<head>
4  <meta charset="utf-8">
5  <meta name="viewport" content="width=device-width">
6  <meta name="nodejs.org:node-version" content="v18.20.1">
7  <title>Inspector | Node.js v18.20.1 Documentation</title>
8  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic&display=fallback">
9  <link rel="stylesheet" href="assets/style.css">
10  <link rel="stylesheet" href="assets/hljs.css">
11  <link rel="canonical" href="https://nodejs.org/api/inspector.html">
12  <script async defer src="assets/api.js" type="text/javascript"></script>
13
14</head>
15<body class="alt apidoc" id="api-section-inspector">
16  <div id="content" class="clearfix">
17    <div id="column2" class="interior">
18      <div id="intro" class="interior">
19        <a href="/" title="Go back to the home page">
20          Node.js
21        </a>
22      </div>
23      <ul>
24<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
25<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
26</ul>
27<hr class="line">
28<ul>
29<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
30<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
31<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
32<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
33<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
34<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
35<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
36<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
37<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
38<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
39<li><a href="console.html" class="nav-console">Console</a></li>
40<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
41<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
42<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
43<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
44<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
45<li><a href="dns.html" class="nav-dns">DNS</a></li>
46<li><a href="domain.html" class="nav-domain">Domain</a></li>
47<li><a href="errors.html" class="nav-errors">Errors</a></li>
48<li><a href="events.html" class="nav-events">Events</a></li>
49<li><a href="fs.html" class="nav-fs">File system</a></li>
50<li><a href="globals.html" class="nav-globals">Globals</a></li>
51<li><a href="http.html" class="nav-http">HTTP</a></li>
52<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
53<li><a href="https.html" class="nav-https">HTTPS</a></li>
54<li><a href="inspector.html" class="nav-inspector active">Inspector</a></li>
55<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
56<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
57<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
58<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
59<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
60<li><a href="net.html" class="nav-net">Net</a></li>
61<li><a href="os.html" class="nav-os">OS</a></li>
62<li><a href="path.html" class="nav-path">Path</a></li>
63<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
64<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
65<li><a href="process.html" class="nav-process">Process</a></li>
66<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
67<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
68<li><a href="readline.html" class="nav-readline">Readline</a></li>
69<li><a href="repl.html" class="nav-repl">REPL</a></li>
70<li><a href="report.html" class="nav-report">Report</a></li>
71<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
72<li><a href="stream.html" class="nav-stream">Stream</a></li>
73<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
74<li><a href="test.html" class="nav-test">Test runner</a></li>
75<li><a href="timers.html" class="nav-timers">Timers</a></li>
76<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
77<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
78<li><a href="tty.html" class="nav-tty">TTY</a></li>
79<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
80<li><a href="url.html" class="nav-url">URL</a></li>
81<li><a href="util.html" class="nav-util">Utilities</a></li>
82<li><a href="v8.html" class="nav-v8">V8</a></li>
83<li><a href="vm.html" class="nav-vm">VM</a></li>
84<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
85<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
86<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
87<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
88<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
89</ul>
90<hr class="line">
91<ul>
92<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
93</ul>
94    </div>
95
96    <div id="column1" data-id="inspector" class="interior">
97      <header class="header">
98        <div class="header-container">
99          <h1>Node.js v18.20.1 documentation</h1>
100          <button class="theme-toggle-btn" id="theme-toggle-btn" title="Toggle dark mode/light mode" aria-label="Toggle dark mode/light mode" hidden>
101            <svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" height="24" width="24">
102              <path fill="none" d="M0 0h24v24H0z" />
103              <path d="M11.1 12.08c-2.33-4.51-.5-8.48.53-10.07C6.27 2.2 1.98 6.59 1.98 12c0 .14.02.28.02.42.62-.27 1.29-.42 2-.42 1.66 0 3.18.83 4.1 2.15A4.01 4.01 0 0111 18c0 1.52-.87 2.83-2.12 3.51.98.32 2.03.5 3.11.5 3.5 0 6.58-1.8 8.37-4.52-2.36.23-6.98-.97-9.26-5.41z"/>
104              <path d="M7 16h-.18C6.4 14.84 5.3 14 4 14c-1.66 0-3 1.34-3 3s1.34 3 3 3h3c1.1 0 2-.9 2-2s-.9-2-2-2z"/>
105            </svg>
106            <svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" height="24" width="24">
107              <path d="M0 0h24v24H0z" fill="none" />
108              <path d="M6.76 4.84l-1.8-1.79-1.41 1.41 1.79 1.79 1.42-1.41zM4 10.5H1v2h3v-2zm9-9.95h-2V3.5h2V.55zm7.45 3.91l-1.41-1.41-1.79 1.79 1.41 1.41 1.79-1.79zm-3.21 13.7l1.79 1.8 1.41-1.41-1.8-1.79-1.4 1.4zM20 10.5v2h3v-2h-3zm-8-5c-3.31 0-6 2.69-6 6s2.69 6 6 6 6-2.69 6-6-2.69-6-6-6zm-1 16.95h2V19.5h-2v2.95zm-7.45-3.91l1.41 1.41 1.79-1.8-1.41-1.41-1.79 1.8z"/>
109            </svg>
110          </button>
111        </div>
112        <div id="gtoc">
113          <ul>
114            <li class="pinned-header">Node.js v18.20.1</li>
115
116    <li class="picker-header">
117      <a href="#">
118        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
119        Table of contents
120      </a>
121
122      <div class="picker"><div class="toc"><ul>
123<li><span class="stability_2"><a href="#inspector">Inspector</a></span>
124<ul>
125<li><a href="#inspectorclose"><code>inspector.close()</code></a></li>
126<li><a href="#inspectorconsole"><code>inspector.console</code></a></li>
127<li><a href="#inspectoropenport-host-wait"><code>inspector.open([port[, host[, wait]]])</code></a></li>
128<li><a href="#inspectorurl"><code>inspector.url()</code></a></li>
129<li><a href="#inspectorwaitfordebugger"><code>inspector.waitForDebugger()</code></a></li>
130<li><a href="#class-inspectorsession">Class: <code>inspector.Session</code></a>
131<ul>
132<li><a href="#new-inspectorsession"><code>new inspector.Session()</code></a></li>
133<li><a href="#event-inspectornotification">Event: <code>'inspectorNotification'</code></a></li>
134<li><a href="#event-inspector-protocol-method">Event: <code>&#x3C;inspector-protocol-method></code>;</a></li>
135<li><a href="#sessionconnect"><code>session.connect()</code></a></li>
136<li><a href="#sessionconnecttomainthread"><code>session.connectToMainThread()</code></a></li>
137<li><a href="#sessiondisconnect"><code>session.disconnect()</code></a></li>
138<li><a href="#sessionpostmethod-params-callback"><code>session.post(method[, params][, callback])</code></a>
139<ul>
140<li><a href="#example-usage">Example usage</a></li>
141</ul>
142</li>
143<li><a href="#cpu-profiler">CPU profiler</a></li>
144<li><a href="#heap-profiler">Heap profiler</a></li>
145</ul>
146</li>
147</ul>
148</li>
149</ul></div></div>
150    </li>
151
152
153    <li class="picker-header">
154      <a href="#">
155        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
156        Index
157      </a>
158
159      <div class="picker"><ul>
160<li><a href="documentation.html" class="nav-documentation">About this documentation</a></li>
161<li><a href="synopsis.html" class="nav-synopsis">Usage and example</a></li>
162
163      <li>
164        <a href="index.html">Index</a>
165      </li>
166    </ul>
167
168<hr class="line">
169<ul>
170<li><a href="assert.html" class="nav-assert">Assertion testing</a></li>
171<li><a href="async_context.html" class="nav-async_context">Asynchronous context tracking</a></li>
172<li><a href="async_hooks.html" class="nav-async_hooks">Async hooks</a></li>
173<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
174<li><a href="addons.html" class="nav-addons">C++ addons</a></li>
175<li><a href="n-api.html" class="nav-n-api">C/C++ addons with Node-API</a></li>
176<li><a href="embedding.html" class="nav-embedding">C++ embedder API</a></li>
177<li><a href="child_process.html" class="nav-child_process">Child processes</a></li>
178<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
179<li><a href="cli.html" class="nav-cli">Command-line options</a></li>
180<li><a href="console.html" class="nav-console">Console</a></li>
181<li><a href="corepack.html" class="nav-corepack">Corepack</a></li>
182<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
183<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
184<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
185<li><a href="diagnostics_channel.html" class="nav-diagnostics_channel">Diagnostics Channel</a></li>
186<li><a href="dns.html" class="nav-dns">DNS</a></li>
187<li><a href="domain.html" class="nav-domain">Domain</a></li>
188<li><a href="errors.html" class="nav-errors">Errors</a></li>
189<li><a href="events.html" class="nav-events">Events</a></li>
190<li><a href="fs.html" class="nav-fs">File system</a></li>
191<li><a href="globals.html" class="nav-globals">Globals</a></li>
192<li><a href="http.html" class="nav-http">HTTP</a></li>
193<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
194<li><a href="https.html" class="nav-https">HTTPS</a></li>
195<li><a href="inspector.html" class="nav-inspector active">Inspector</a></li>
196<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
197<li><a href="modules.html" class="nav-modules">Modules: CommonJS modules</a></li>
198<li><a href="esm.html" class="nav-esm">Modules: ECMAScript modules</a></li>
199<li><a href="module.html" class="nav-module">Modules: <code>node:module</code> API</a></li>
200<li><a href="packages.html" class="nav-packages">Modules: Packages</a></li>
201<li><a href="net.html" class="nav-net">Net</a></li>
202<li><a href="os.html" class="nav-os">OS</a></li>
203<li><a href="path.html" class="nav-path">Path</a></li>
204<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance hooks</a></li>
205<li><a href="permissions.html" class="nav-permissions">Permissions</a></li>
206<li><a href="process.html" class="nav-process">Process</a></li>
207<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
208<li><a href="querystring.html" class="nav-querystring">Query strings</a></li>
209<li><a href="readline.html" class="nav-readline">Readline</a></li>
210<li><a href="repl.html" class="nav-repl">REPL</a></li>
211<li><a href="report.html" class="nav-report">Report</a></li>
212<li><a href="single-executable-applications.html" class="nav-single-executable-applications">Single executable applications</a></li>
213<li><a href="stream.html" class="nav-stream">Stream</a></li>
214<li><a href="string_decoder.html" class="nav-string_decoder">String decoder</a></li>
215<li><a href="test.html" class="nav-test">Test runner</a></li>
216<li><a href="timers.html" class="nav-timers">Timers</a></li>
217<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
218<li><a href="tracing.html" class="nav-tracing">Trace events</a></li>
219<li><a href="tty.html" class="nav-tty">TTY</a></li>
220<li><a href="dgram.html" class="nav-dgram">UDP/datagram</a></li>
221<li><a href="url.html" class="nav-url">URL</a></li>
222<li><a href="util.html" class="nav-util">Utilities</a></li>
223<li><a href="v8.html" class="nav-v8">V8</a></li>
224<li><a href="vm.html" class="nav-vm">VM</a></li>
225<li><a href="wasi.html" class="nav-wasi">WASI</a></li>
226<li><a href="webcrypto.html" class="nav-webcrypto">Web Crypto API</a></li>
227<li><a href="webstreams.html" class="nav-webstreams">Web Streams API</a></li>
228<li><a href="worker_threads.html" class="nav-worker_threads">Worker threads</a></li>
229<li><a href="zlib.html" class="nav-zlib">Zlib</a></li>
230</ul>
231<hr class="line">
232<ul>
233<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">Code repository and issue tracker</a></li>
234</ul></div>
235    </li>
236
237
238    <li class="picker-header">
239      <a href="#">
240        <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
241        Other versions
242      </a>
243      <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v21.x/api/inspector.html">21.x</a></li>
244<li><a href="https://nodejs.org/docs/latest-v20.x/api/inspector.html">20.x <b>LTS</b></a></li>
245<li><a href="https://nodejs.org/docs/latest-v19.x/api/inspector.html">19.x</a></li>
246<li><a href="https://nodejs.org/docs/latest-v18.x/api/inspector.html">18.x <b>LTS</b></a></li>
247<li><a href="https://nodejs.org/docs/latest-v17.x/api/inspector.html">17.x</a></li>
248<li><a href="https://nodejs.org/docs/latest-v16.x/api/inspector.html">16.x</a></li>
249<li><a href="https://nodejs.org/docs/latest-v15.x/api/inspector.html">15.x</a></li>
250<li><a href="https://nodejs.org/docs/latest-v14.x/api/inspector.html">14.x</a></li>
251<li><a href="https://nodejs.org/docs/latest-v13.x/api/inspector.html">13.x</a></li>
252<li><a href="https://nodejs.org/docs/latest-v12.x/api/inspector.html">12.x</a></li>
253<li><a href="https://nodejs.org/docs/latest-v11.x/api/inspector.html">11.x</a></li>
254<li><a href="https://nodejs.org/docs/latest-v10.x/api/inspector.html">10.x</a></li>
255<li><a href="https://nodejs.org/docs/latest-v9.x/api/inspector.html">9.x</a></li>
256<li><a href="https://nodejs.org/docs/latest-v8.x/api/inspector.html">8.x</a></li></ol></div>
257    </li>
258
259            <li class="picker-header">
260              <a href="#">
261                <span class="collapsed-arrow">&#x25ba;</span><span class="expanded-arrow">&#x25bc;</span>
262                Options
263              </a>
264
265              <div class="picker">
266                <ul>
267                  <li>
268                    <a href="all.html">View on single page</a>
269                  </li>
270                  <li>
271                    <a href="inspector.json">View as JSON</a>
272                  </li>
273                  <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/inspector.md">Edit on GitHub</a></li>
274                </ul>
275              </div>
276            </li>
277          </ul>
278        </div>
279        <hr>
280      </header>
281
282      <details id="toc" open><summary>Table of contents</summary><ul>
283<li><span class="stability_2"><a href="#inspector">Inspector</a></span>
284<ul>
285<li><a href="#inspectorclose"><code>inspector.close()</code></a></li>
286<li><a href="#inspectorconsole"><code>inspector.console</code></a></li>
287<li><a href="#inspectoropenport-host-wait"><code>inspector.open([port[, host[, wait]]])</code></a></li>
288<li><a href="#inspectorurl"><code>inspector.url()</code></a></li>
289<li><a href="#inspectorwaitfordebugger"><code>inspector.waitForDebugger()</code></a></li>
290<li><a href="#class-inspectorsession">Class: <code>inspector.Session</code></a>
291<ul>
292<li><a href="#new-inspectorsession"><code>new inspector.Session()</code></a></li>
293<li><a href="#event-inspectornotification">Event: <code>'inspectorNotification'</code></a></li>
294<li><a href="#event-inspector-protocol-method">Event: <code>&#x3C;inspector-protocol-method></code>;</a></li>
295<li><a href="#sessionconnect"><code>session.connect()</code></a></li>
296<li><a href="#sessionconnecttomainthread"><code>session.connectToMainThread()</code></a></li>
297<li><a href="#sessiondisconnect"><code>session.disconnect()</code></a></li>
298<li><a href="#sessionpostmethod-params-callback"><code>session.post(method[, params][, callback])</code></a>
299<ul>
300<li><a href="#example-usage">Example usage</a></li>
301</ul>
302</li>
303<li><a href="#cpu-profiler">CPU profiler</a></li>
304<li><a href="#heap-profiler">Heap profiler</a></li>
305</ul>
306</li>
307</ul>
308</li>
309</ul></details>
310
311      <div id="apicontent">
312        <h2>Inspector<span><a class="mark" href="#inspector" id="inspector">#</a></span><a aria-hidden="true" class="legacy" id="inspector_inspector"></a></h2>
313
314<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p>
315<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.20.1/lib/inspector.js">lib/inspector.js</a></p>
316<p>The <code>node:inspector</code> module provides an API for interacting with the V8
317inspector.</p>
318<p>It can be accessed using:</p>
319<pre><code class="language-js"><span class="hljs-keyword">const</span> inspector = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:inspector'</span>);</code> <button class="copy-button">copy</button></pre>
320<section><h3><code>inspector.close()</code><span><a class="mark" href="#inspectorclose" id="inspectorclose">#</a></span><a aria-hidden="true" class="legacy" id="inspector_inspector_close"></a></h3>
321<div class="api_metadata">
322<details class="changelog"><summary>History</summary>
323<table>
324<tbody><tr><th>Version</th><th>Changes</th></tr>
325<tr><td>v18.10.0</td>
326<td><p>The API is exposed in the worker threads.</p></td></tr>
327<tr><td>v9.0.0</td>
328<td><p><span>Added in: v9.0.0</span></p></td></tr>
329</tbody></table>
330</details>
331</div>
332<p>Deactivate the inspector. Blocks until there are no active connections.</p>
333<p>When using <code>Session</code>, the object outputted by the console API will not be
334released, unless we performed manually <code>Runtime.DiscardConsoleEntries</code>
335command.</p>
336</section><section><h3><code>inspector.console</code><span><a class="mark" href="#inspectorconsole" id="inspectorconsole">#</a></span><a aria-hidden="true" class="legacy" id="inspector_inspector_console"></a></h3>
337<ul>
338<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> An object to send messages to the remote inspector console.</li>
339</ul>
340<pre><code class="language-js"><span class="hljs-built_in">require</span>(<span class="hljs-string">'node:inspector'</span>).<span class="hljs-property">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'a message'</span>);</code> <button class="copy-button">copy</button></pre>
341<p>The inspector console does not have API parity with Node.js
342console.</p>
343</section><section><h3><code>inspector.open([port[, host[, wait]]])</code><span><a class="mark" href="#inspectoropenport-host-wait" id="inspectoropenport-host-wait">#</a></span><a aria-hidden="true" class="legacy" id="inspector_inspector_open_port_host_wait"></a></h3>
344<ul>
345<li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type">&#x3C;number></a> Port to listen on for inspector connections. Optional.
346<strong>Default:</strong> what was specified on the CLI.</li>
347<li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> Host to listen on for inspector connections. Optional.
348<strong>Default:</strong> what was specified on the CLI.</li>
349<li><code>wait</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type">&#x3C;boolean></a> Block until a client has connected. Optional.
350<strong>Default:</strong> <code>false</code>.</li>
351</ul>
352<p>Activate inspector on host and port. Equivalent to
353<code>node --inspect=[[host:]port]</code>, but can be done programmatically after node has
354started.</p>
355<p>If wait is <code>true</code>, will block until a client has connected to the inspect port
356and flow control has been passed to the debugger client.</p>
357<p>See the <a href="cli.html#warning-binding-inspector-to-a-public-ipport-combination-is-insecure">security warning</a> regarding the <code>host</code>
358parameter usage.</p>
359</section><section><h3><code>inspector.url()</code><span><a class="mark" href="#inspectorurl" id="inspectorurl">#</a></span><a aria-hidden="true" class="legacy" id="inspector_inspector_url"></a></h3>
360<ul>
361<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type">&#x3C;undefined></a></li>
362</ul>
363<p>Return the URL of the active inspector, or <code>undefined</code> if there is none.</p>
364<pre><code class="language-console"><span class="hljs-meta prompt_">$ </span><span class="language-bash">node --inspect -p <span class="hljs-string">'inspector.url()'</span></span>
365Debugger listening on ws://127.0.0.1:9229/166e272e-7a30-4d09-97ce-f1c012b43c34
366For help, see: https://nodejs.org/en/docs/inspector
367ws://127.0.0.1:9229/166e272e-7a30-4d09-97ce-f1c012b43c34
368<span class="hljs-meta prompt_">
369$ </span><span class="language-bash">node --inspect=localhost:3000 -p <span class="hljs-string">'inspector.url()'</span></span>
370Debugger listening on ws://localhost:3000/51cf8d0e-3c36-4c59-8efd-54519839e56a
371For help, see: https://nodejs.org/en/docs/inspector
372ws://localhost:3000/51cf8d0e-3c36-4c59-8efd-54519839e56a
373<span class="hljs-meta prompt_">
374$ </span><span class="language-bash">node -p <span class="hljs-string">'inspector.url()'</span></span>
375undefined</code> <button class="copy-button">copy</button></pre>
376</section><section><h3><code>inspector.waitForDebugger()</code><span><a class="mark" href="#inspectorwaitfordebugger" id="inspectorwaitfordebugger">#</a></span><a aria-hidden="true" class="legacy" id="inspector_inspector_waitfordebugger"></a></h3>
377<div class="api_metadata">
378<span>Added in: v12.7.0</span>
379</div>
380<p>Blocks until a client (existing or connected later) has sent
381<code>Runtime.runIfWaitingForDebugger</code> command.</p>
382<p>An exception will be thrown if there is no active inspector.</p>
383</section><section><h3>Class: <code>inspector.Session</code><span><a class="mark" href="#class-inspectorsession" id="class-inspectorsession">#</a></span><a aria-hidden="true" class="legacy" id="inspector_class_inspector_session"></a></h3>
384<ul>
385<li>Extends: <a href="events.html#class-eventemitter" class="type">&#x3C;EventEmitter></a></li>
386</ul>
387<p>The <code>inspector.Session</code> is used for dispatching messages to the V8 inspector
388back-end and receiving message responses and notifications.</p>
389<h4><code>new inspector.Session()</code><span><a class="mark" href="#new-inspectorsession" id="new-inspectorsession">#</a></span><a aria-hidden="true" class="legacy" id="inspector_new_inspector_session"></a></h4>
390<div class="api_metadata">
391<span>Added in: v8.0.0</span>
392</div>
393<p>Create a new instance of the <code>inspector.Session</code> class. The inspector session
394needs to be connected through <a href="#sessionconnect"><code>session.connect()</code></a> before the messages
395can be dispatched to the inspector backend.</p>
396<p>When using <code>Session</code>, the object outputted by the console API will not be
397released, unless we performed manually <code>Runtime.DiscardConsoleEntries</code>
398command.</p>
399<h4>Event: <code>'inspectorNotification'</code><span><a class="mark" href="#event-inspectornotification" id="event-inspectornotification">#</a></span><a aria-hidden="true" class="legacy" id="inspector_event_inspectornotification"></a></h4>
400<div class="api_metadata">
401<span>Added in: v8.0.0</span>
402</div>
403<ul>
404<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> The notification message object</li>
405</ul>
406<p>Emitted when any notification from the V8 Inspector is received.</p>
407<pre><code class="language-js">session.<span class="hljs-title function_">on</span>(<span class="hljs-string">'inspectorNotification'</span>, <span class="hljs-function">(<span class="hljs-params">message</span>) =></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(message.<span class="hljs-property">method</span>));
408<span class="hljs-comment">// Debugger.paused</span>
409<span class="hljs-comment">// Debugger.resumed</span></code> <button class="copy-button">copy</button></pre>
410<p>It is also possible to subscribe only to notifications with specific method:</p>
411<h4>Event: <code>&#x3C;inspector-protocol-method></code>;<span><a class="mark" href="#event-inspector-protocol-method" id="event-inspector-protocol-method">#</a></span><a aria-hidden="true" class="legacy" id="inspector_event_inspector_protocol_method"></a></h4>
412<div class="api_metadata">
413<span>Added in: v8.0.0</span>
414</div>
415<ul>
416<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a> The notification message object</li>
417</ul>
418<p>Emitted when an inspector notification is received that has its method field set
419to the <code>&#x3C;inspector-protocol-method></code> value.</p>
420<p>The following snippet installs a listener on the <a href="https://chromedevtools.github.io/devtools-protocol/v8/Debugger#event-paused"><code>'Debugger.paused'</code></a>
421event, and prints the reason for program suspension whenever program
422execution is suspended (through breakpoints, for example):</p>
423<pre><code class="language-js">session.<span class="hljs-title function_">on</span>(<span class="hljs-string">'Debugger.paused'</span>, <span class="hljs-function">(<span class="hljs-params">{ params }</span>) =></span> {
424  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-property">hitBreakpoints</span>);
425});
426<span class="hljs-comment">// [ '/the/file/that/has/the/breakpoint.js:11:0' ]</span></code> <button class="copy-button">copy</button></pre>
427<h4><code>session.connect()</code><span><a class="mark" href="#sessionconnect" id="sessionconnect">#</a></span><a aria-hidden="true" class="legacy" id="inspector_session_connect"></a></h4>
428<div class="api_metadata">
429<span>Added in: v8.0.0</span>
430</div>
431<p>Connects a session to the inspector back-end.</p>
432<h4><code>session.connectToMainThread()</code><span><a class="mark" href="#sessionconnecttomainthread" id="sessionconnecttomainthread">#</a></span><a aria-hidden="true" class="legacy" id="inspector_session_connecttomainthread"></a></h4>
433<div class="api_metadata">
434<span>Added in: v12.11.0</span>
435</div>
436<p>Connects a session to the main thread inspector back-end. An exception will
437be thrown if this API was not called on a Worker thread.</p>
438<h4><code>session.disconnect()</code><span><a class="mark" href="#sessiondisconnect" id="sessiondisconnect">#</a></span><a aria-hidden="true" class="legacy" id="inspector_session_disconnect"></a></h4>
439<div class="api_metadata">
440<span>Added in: v8.0.0</span>
441</div>
442<p>Immediately close the session. All pending message callbacks will be called
443with an error. <a href="#sessionconnect"><code>session.connect()</code></a> will need to be called to be able to send
444messages again. Reconnected session will lose all inspector state, such as
445enabled agents or configured breakpoints.</p>
446<h4><code>session.post(method[, params][, callback])</code><span><a class="mark" href="#sessionpostmethod-params-callback" id="sessionpostmethod-params-callback">#</a></span><a aria-hidden="true" class="legacy" id="inspector_session_post_method_params_callback"></a></h4>
447<div class="api_metadata">
448<details class="changelog"><summary>History</summary>
449<table>
450<tbody><tr><th>Version</th><th>Changes</th></tr>
451<tr><td>v18.0.0</td>
452<td><p>Passing an invalid callback to the <code>callback</code> argument now throws <code>ERR_INVALID_ARG_TYPE</code> instead of <code>ERR_INVALID_CALLBACK</code>.</p></td></tr>
453<tr><td>v8.0.0</td>
454<td><p><span>Added in: v8.0.0</span></p></td></tr>
455</tbody></table>
456</details>
457</div>
458<ul>
459<li><code>method</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type">&#x3C;string></a></li>
460<li><code>params</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type">&#x3C;Object></a></li>
461<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type">&#x3C;Function></a></li>
462</ul>
463<p>Posts a message to the inspector back-end. <code>callback</code> will be notified when
464a response is received. <code>callback</code> is a function that accepts two optional
465arguments: error and message-specific result.</p>
466<pre><code class="language-js">session.<span class="hljs-title function_">post</span>(<span class="hljs-string">'Runtime.evaluate'</span>, { <span class="hljs-attr">expression</span>: <span class="hljs-string">'2 + 2'</span> },
467             <span class="hljs-function">(<span class="hljs-params">error, { result }</span>) =></span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(result));
468<span class="hljs-comment">// Output: { type: 'number', value: 4, description: '4' }</span></code> <button class="copy-button">copy</button></pre>
469<p>The latest version of the V8 inspector protocol is published on the
470<a href="https://chromedevtools.github.io/devtools-protocol/v8/">Chrome DevTools Protocol Viewer</a>.</p>
471<p>Node.js inspector supports all the Chrome DevTools Protocol domains declared
472by V8. Chrome DevTools Protocol domain provides an interface for interacting
473with one of the runtime agents used to inspect the application state and listen
474to the run-time events.</p>
475<p>You can not set <code>reportProgress</code> to <code>true</code> when sending a
476<code>HeapProfiler.takeHeapSnapshot</code> or <code>HeapProfiler.stopTrackingHeapObjects</code>
477command to V8.</p>
478<h5>Example usage<span><a class="mark" href="#example-usage" id="example-usage">#</a></span><a aria-hidden="true" class="legacy" id="inspector_example_usage"></a></h5>
479<p>Apart from the debugger, various V8 Profilers are available through the DevTools
480protocol.</p>
481<h4>CPU profiler<span><a class="mark" href="#cpu-profiler" id="cpu-profiler">#</a></span><a aria-hidden="true" class="legacy" id="inspector_cpu_profiler"></a></h4>
482<p>Here's an example showing how to use the <a href="https://chromedevtools.github.io/devtools-protocol/v8/Profiler">CPU Profiler</a>:</p>
483<pre><code class="language-js"><span class="hljs-keyword">const</span> inspector = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:inspector'</span>);
484<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>);
485<span class="hljs-keyword">const</span> session = <span class="hljs-keyword">new</span> inspector.<span class="hljs-title class_">Session</span>();
486session.<span class="hljs-title function_">connect</span>();
487
488session.<span class="hljs-title function_">post</span>(<span class="hljs-string">'Profiler.enable'</span>, <span class="hljs-function">() =></span> {
489  session.<span class="hljs-title function_">post</span>(<span class="hljs-string">'Profiler.start'</span>, <span class="hljs-function">() =></span> {
490    <span class="hljs-comment">// Invoke business logic under measurement here...</span>
491
492    <span class="hljs-comment">// some time later...</span>
493    session.<span class="hljs-title function_">post</span>(<span class="hljs-string">'Profiler.stop'</span>, <span class="hljs-function">(<span class="hljs-params">err, { profile }</span>) =></span> {
494      <span class="hljs-comment">// Write profile to disk, upload, etc.</span>
495      <span class="hljs-keyword">if</span> (!err) {
496        fs.<span class="hljs-title function_">writeFileSync</span>(<span class="hljs-string">'./profile.cpuprofile'</span>, <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>(profile));
497      }
498    });
499  });
500});</code> <button class="copy-button">copy</button></pre>
501<h4>Heap profiler<span><a class="mark" href="#heap-profiler" id="heap-profiler">#</a></span><a aria-hidden="true" class="legacy" id="inspector_heap_profiler"></a></h4>
502<p>Here's an example showing how to use the <a href="https://chromedevtools.github.io/devtools-protocol/v8/HeapProfiler">Heap Profiler</a>:</p>
503<pre><code class="language-js"><span class="hljs-keyword">const</span> inspector = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:inspector'</span>);
504<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>);
505<span class="hljs-keyword">const</span> session = <span class="hljs-keyword">new</span> inspector.<span class="hljs-title class_">Session</span>();
506
507<span class="hljs-keyword">const</span> fd = fs.<span class="hljs-title function_">openSync</span>(<span class="hljs-string">'profile.heapsnapshot'</span>, <span class="hljs-string">'w'</span>);
508
509session.<span class="hljs-title function_">connect</span>();
510
511session.<span class="hljs-title function_">on</span>(<span class="hljs-string">'HeapProfiler.addHeapSnapshotChunk'</span>, <span class="hljs-function">(<span class="hljs-params">m</span>) =></span> {
512  fs.<span class="hljs-title function_">writeSync</span>(fd, m.<span class="hljs-property">params</span>.<span class="hljs-property">chunk</span>);
513});
514
515session.<span class="hljs-title function_">post</span>(<span class="hljs-string">'HeapProfiler.takeHeapSnapshot'</span>, <span class="hljs-literal">null</span>, <span class="hljs-function">(<span class="hljs-params">err, r</span>) =></span> {
516  <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'HeapProfiler.takeHeapSnapshot done:'</span>, err, r);
517  session.<span class="hljs-title function_">disconnect</span>();
518  fs.<span class="hljs-title function_">closeSync</span>(fd);
519});</code> <button class="copy-button">copy</button></pre></section>
520        <!-- API END -->
521      </div>
522    </div>
523  </div>
524</body>
525</html>
526