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.18.2"> 7 <title>Inspector | Node.js v18.18.2 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.18.2 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.18.2</li> 115 116 <li class="picker-header"> 117 <a href="#"> 118 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</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><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">►</span><span class="expanded-arrow">▼</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">►</span><span class="expanded-arrow">▼</span> 241 Other versions 242 </a> 243 <div class="picker"><ol id="alt-docs"><li><a href="https://nodejs.org/docs/latest-v20.x/api/inspector.html">20.x</a></li> 244<li><a href="https://nodejs.org/docs/latest-v19.x/api/inspector.html">19.x</a></li> 245<li><a href="https://nodejs.org/docs/latest-v18.x/api/inspector.html">18.x <b>LTS</b></a></li> 246<li><a href="https://nodejs.org/docs/latest-v17.x/api/inspector.html">17.x</a></li> 247<li><a href="https://nodejs.org/docs/latest-v16.x/api/inspector.html">16.x</a></li> 248<li><a href="https://nodejs.org/docs/latest-v15.x/api/inspector.html">15.x</a></li> 249<li><a href="https://nodejs.org/docs/latest-v14.x/api/inspector.html">14.x</a></li> 250<li><a href="https://nodejs.org/docs/latest-v13.x/api/inspector.html">13.x</a></li> 251<li><a href="https://nodejs.org/docs/latest-v12.x/api/inspector.html">12.x</a></li> 252<li><a href="https://nodejs.org/docs/latest-v11.x/api/inspector.html">11.x</a></li> 253<li><a href="https://nodejs.org/docs/latest-v10.x/api/inspector.html">10.x</a></li> 254<li><a href="https://nodejs.org/docs/latest-v9.x/api/inspector.html">9.x</a></li> 255<li><a href="https://nodejs.org/docs/latest-v8.x/api/inspector.html">8.x</a></li></ol></div> 256 </li> 257 258 <li class="picker-header"> 259 <a href="#"> 260 <span class="collapsed-arrow">►</span><span class="expanded-arrow">▼</span> 261 Options 262 </a> 263 264 <div class="picker"> 265 <ul> 266 <li> 267 <a href="all.html">View on single page</a> 268 </li> 269 <li> 270 <a href="inspector.json">View as JSON</a> 271 </li> 272 <li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/main/doc/api/inspector.md">Edit on GitHub</a></li> 273 </ul> 274 </div> 275 </li> 276 </ul> 277 </div> 278 <hr> 279 </header> 280 281 <details id="toc" open><summary>Table of contents</summary><ul> 282<li><span class="stability_2"><a href="#inspector">Inspector</a></span> 283<ul> 284<li><a href="#inspectorclose"><code>inspector.close()</code></a></li> 285<li><a href="#inspectorconsole"><code>inspector.console</code></a></li> 286<li><a href="#inspectoropenport-host-wait"><code>inspector.open([port[, host[, wait]]])</code></a></li> 287<li><a href="#inspectorurl"><code>inspector.url()</code></a></li> 288<li><a href="#inspectorwaitfordebugger"><code>inspector.waitForDebugger()</code></a></li> 289<li><a href="#class-inspectorsession">Class: <code>inspector.Session</code></a> 290<ul> 291<li><a href="#new-inspectorsession"><code>new inspector.Session()</code></a></li> 292<li><a href="#event-inspectornotification">Event: <code>'inspectorNotification'</code></a></li> 293<li><a href="#event-inspector-protocol-method">Event: <code><inspector-protocol-method></code>;</a></li> 294<li><a href="#sessionconnect"><code>session.connect()</code></a></li> 295<li><a href="#sessionconnecttomainthread"><code>session.connectToMainThread()</code></a></li> 296<li><a href="#sessiondisconnect"><code>session.disconnect()</code></a></li> 297<li><a href="#sessionpostmethod-params-callback"><code>session.post(method[, params][, callback])</code></a> 298<ul> 299<li><a href="#example-usage">Example usage</a></li> 300</ul> 301</li> 302<li><a href="#cpu-profiler">CPU profiler</a></li> 303<li><a href="#heap-profiler">Heap profiler</a></li> 304</ul> 305</li> 306</ul> 307</li> 308</ul></details> 309 310 <div id="apicontent"> 311 <h2>Inspector<span><a class="mark" href="#inspector" id="inspector">#</a></span><a aria-hidden="true" class="legacy" id="inspector_inspector"></a></h2> 312 313<p></p><div class="api_stability api_stability_2"><a href="documentation.html#stability-index">Stability: 2</a> - Stable</div><p></p> 314<p><strong>Source Code:</strong> <a href="https://github.com/nodejs/node/blob/v18.18.2/lib/inspector.js">lib/inspector.js</a></p> 315<p>The <code>node:inspector</code> module provides an API for interacting with the V8 316inspector.</p> 317<p>It can be accessed using:</p> 318<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> 319<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> 320<div class="api_metadata"> 321<details class="changelog"><summary>History</summary> 322<table> 323<tbody><tr><th>Version</th><th>Changes</th></tr> 324<tr><td>v18.10.0</td> 325<td><p>The API is exposed in the worker threads.</p></td></tr> 326<tr><td>v9.0.0</td> 327<td><p><span>Added in: v9.0.0</span></p></td></tr> 328</tbody></table> 329</details> 330</div> 331<p>Deactivate the inspector. Blocks until there are no active connections.</p> 332<p>When using <code>Session</code>, the object outputted by the console API will not be 333released, unless we performed manually <code>Runtime.DiscardConsoleEntries</code> 334command.</p> 335</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> 336<ul> 337<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> An object to send messages to the remote inspector console.</li> 338</ul> 339<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> 340<p>The inspector console does not have API parity with Node.js 341console.</p> 342</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> 343<ul> 344<li><code>port</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Port to listen on for inspector connections. Optional. 345<strong>Default:</strong> what was specified on the CLI.</li> 346<li><code>host</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> Host to listen on for inspector connections. Optional. 347<strong>Default:</strong> what was specified on the CLI.</li> 348<li><code>wait</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Block until a client has connected. Optional. 349<strong>Default:</strong> <code>false</code>.</li> 350</ul> 351<p>Activate inspector on host and port. Equivalent to 352<code>node --inspect=[[host:]port]</code>, but can be done programmatically after node has 353started.</p> 354<p>If wait is <code>true</code>, will block until a client has connected to the inspect port 355and flow control has been passed to the debugger client.</p> 356<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> 357parameter usage.</p> 358</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> 359<ul> 360<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Undefined_type" class="type"><undefined></a></li> 361</ul> 362<p>Return the URL of the active inspector, or <code>undefined</code> if there is none.</p> 363<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> 364Debugger listening on ws://127.0.0.1:9229/166e272e-7a30-4d09-97ce-f1c012b43c34 365For help, see: https://nodejs.org/en/docs/inspector 366ws://127.0.0.1:9229/166e272e-7a30-4d09-97ce-f1c012b43c34 367<span class="hljs-meta prompt_"> 368$ </span><span class="language-bash">node --inspect=localhost:3000 -p <span class="hljs-string">'inspector.url()'</span></span> 369Debugger listening on ws://localhost:3000/51cf8d0e-3c36-4c59-8efd-54519839e56a 370For help, see: https://nodejs.org/en/docs/inspector 371ws://localhost:3000/51cf8d0e-3c36-4c59-8efd-54519839e56a 372<span class="hljs-meta prompt_"> 373$ </span><span class="language-bash">node -p <span class="hljs-string">'inspector.url()'</span></span> 374undefined</code> <button class="copy-button">copy</button></pre> 375</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> 376<div class="api_metadata"> 377<span>Added in: v12.7.0</span> 378</div> 379<p>Blocks until a client (existing or connected later) has sent 380<code>Runtime.runIfWaitingForDebugger</code> command.</p> 381<p>An exception will be thrown if there is no active inspector.</p> 382</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> 383<ul> 384<li>Extends: <a href="events.html#class-eventemitter" class="type"><EventEmitter></a></li> 385</ul> 386<p>The <code>inspector.Session</code> is used for dispatching messages to the V8 inspector 387back-end and receiving message responses and notifications.</p> 388<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> 389<div class="api_metadata"> 390<span>Added in: v8.0.0</span> 391</div> 392<p>Create a new instance of the <code>inspector.Session</code> class. The inspector session 393needs to be connected through <a href="#sessionconnect"><code>session.connect()</code></a> before the messages 394can be dispatched to the inspector backend.</p> 395<p>When using <code>Session</code>, the object outputted by the console API will not be 396released, unless we performed manually <code>Runtime.DiscardConsoleEntries</code> 397command.</p> 398<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> 399<div class="api_metadata"> 400<span>Added in: v8.0.0</span> 401</div> 402<ul> 403<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> The notification message object</li> 404</ul> 405<p>Emitted when any notification from the V8 Inspector is received.</p> 406<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>)); 407<span class="hljs-comment">// Debugger.paused</span> 408<span class="hljs-comment">// Debugger.resumed</span></code> <button class="copy-button">copy</button></pre> 409<p>It is also possible to subscribe only to notifications with specific method:</p> 410<h4>Event: <code><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> 411<div class="api_metadata"> 412<span>Added in: v8.0.0</span> 413</div> 414<ul> 415<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a> The notification message object</li> 416</ul> 417<p>Emitted when an inspector notification is received that has its method field set 418to the <code><inspector-protocol-method></code> value.</p> 419<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> 420event, and prints the reason for program suspension whenever program 421execution is suspended (through breakpoints, for example):</p> 422<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> { 423 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(params.<span class="hljs-property">hitBreakpoints</span>); 424}); 425<span class="hljs-comment">// [ '/the/file/that/has/the/breakpoint.js:11:0' ]</span></code> <button class="copy-button">copy</button></pre> 426<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> 427<div class="api_metadata"> 428<span>Added in: v8.0.0</span> 429</div> 430<p>Connects a session to the inspector back-end.</p> 431<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> 432<div class="api_metadata"> 433<span>Added in: v12.11.0</span> 434</div> 435<p>Connects a session to the main thread inspector back-end. An exception will 436be thrown if this API was not called on a Worker thread.</p> 437<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> 438<div class="api_metadata"> 439<span>Added in: v8.0.0</span> 440</div> 441<p>Immediately close the session. All pending message callbacks will be called 442with an error. <a href="#sessionconnect"><code>session.connect()</code></a> will need to be called to be able to send 443messages again. Reconnected session will lose all inspector state, such as 444enabled agents or configured breakpoints.</p> 445<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> 446<div class="api_metadata"> 447<details class="changelog"><summary>History</summary> 448<table> 449<tbody><tr><th>Version</th><th>Changes</th></tr> 450<tr><td>v18.0.0</td> 451<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> 452<tr><td>v8.0.0</td> 453<td><p><span>Added in: v8.0.0</span></p></td></tr> 454</tbody></table> 455</details> 456</div> 457<ul> 458<li><code>method</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li> 459<li><code>params</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li> 460<li><code>callback</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li> 461</ul> 462<p>Posts a message to the inspector back-end. <code>callback</code> will be notified when 463a response is received. <code>callback</code> is a function that accepts two optional 464arguments: error and message-specific result.</p> 465<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> }, 466 <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)); 467<span class="hljs-comment">// Output: { type: 'number', value: 4, description: '4' }</span></code> <button class="copy-button">copy</button></pre> 468<p>The latest version of the V8 inspector protocol is published on the 469<a href="https://chromedevtools.github.io/devtools-protocol/v8/">Chrome DevTools Protocol Viewer</a>.</p> 470<p>Node.js inspector supports all the Chrome DevTools Protocol domains declared 471by V8. Chrome DevTools Protocol domain provides an interface for interacting 472with one of the runtime agents used to inspect the application state and listen 473to the run-time events.</p> 474<p>You can not set <code>reportProgress</code> to <code>true</code> when sending a 475<code>HeapProfiler.takeHeapSnapshot</code> or <code>HeapProfiler.stopTrackingHeapObjects</code> 476command to V8.</p> 477<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> 478<p>Apart from the debugger, various V8 Profilers are available through the DevTools 479protocol.</p> 480<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> 481<p>Here's an example showing how to use the <a href="https://chromedevtools.github.io/devtools-protocol/v8/Profiler">CPU Profiler</a>:</p> 482<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>); 483<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>); 484<span class="hljs-keyword">const</span> session = <span class="hljs-keyword">new</span> inspector.<span class="hljs-title class_">Session</span>(); 485session.<span class="hljs-title function_">connect</span>(); 486 487session.<span class="hljs-title function_">post</span>(<span class="hljs-string">'Profiler.enable'</span>, <span class="hljs-function">() =></span> { 488 session.<span class="hljs-title function_">post</span>(<span class="hljs-string">'Profiler.start'</span>, <span class="hljs-function">() =></span> { 489 <span class="hljs-comment">// Invoke business logic under measurement here...</span> 490 491 <span class="hljs-comment">// some time later...</span> 492 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> { 493 <span class="hljs-comment">// Write profile to disk, upload, etc.</span> 494 <span class="hljs-keyword">if</span> (!err) { 495 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)); 496 } 497 }); 498 }); 499});</code> <button class="copy-button">copy</button></pre> 500<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> 501<p>Here's an example showing how to use the <a href="https://chromedevtools.github.io/devtools-protocol/v8/HeapProfiler">Heap Profiler</a>:</p> 502<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>); 503<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'node:fs'</span>); 504<span class="hljs-keyword">const</span> session = <span class="hljs-keyword">new</span> inspector.<span class="hljs-title class_">Session</span>(); 505 506<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>); 507 508session.<span class="hljs-title function_">connect</span>(); 509 510session.<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> { 511 fs.<span class="hljs-title function_">writeSync</span>(fd, m.<span class="hljs-property">params</span>.<span class="hljs-property">chunk</span>); 512}); 513 514session.<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> { 515 <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'HeapProfiler.takeHeapSnapshot done:'</span>, err, r); 516 session.<span class="hljs-title function_">disconnect</span>(); 517 fs.<span class="hljs-title function_">closeSync</span>(fd); 518});</code> <button class="copy-button">copy</button></pre></section> 519 <!-- API END --> 520 </div> 521 </div> 522 </div> 523</body> 524</html> 525