11cb0ef41Sopenharmony_ci# Inspector 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ci<!--introduced_in=v8.0.0--> 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ci> Stability: 2 - Stable 61cb0ef41Sopenharmony_ci 71cb0ef41Sopenharmony_ci<!-- source_link=lib/inspector.js --> 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ciThe `node:inspector` module provides an API for interacting with the V8 101cb0ef41Sopenharmony_ciinspector. 111cb0ef41Sopenharmony_ci 121cb0ef41Sopenharmony_ciIt can be accessed using: 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ci```js 151cb0ef41Sopenharmony_ciconst inspector = require('node:inspector'); 161cb0ef41Sopenharmony_ci``` 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_ci## `inspector.close()` 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_ci<!-- YAML 211cb0ef41Sopenharmony_ciadded: v9.0.0 221cb0ef41Sopenharmony_cichanges: 231cb0ef41Sopenharmony_ci - version: v18.10.0 241cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/44489 251cb0ef41Sopenharmony_ci description: The API is exposed in the worker threads. 261cb0ef41Sopenharmony_ci--> 271cb0ef41Sopenharmony_ci 281cb0ef41Sopenharmony_ciDeactivate the inspector. Blocks until there are no active connections. 291cb0ef41Sopenharmony_ci 301cb0ef41Sopenharmony_ciWhen using `Session`, the object outputted by the console API will not be 311cb0ef41Sopenharmony_cireleased, unless we performed manually `Runtime.DiscardConsoleEntries` 321cb0ef41Sopenharmony_cicommand. 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ci## `inspector.console` 351cb0ef41Sopenharmony_ci 361cb0ef41Sopenharmony_ci* {Object} An object to send messages to the remote inspector console. 371cb0ef41Sopenharmony_ci 381cb0ef41Sopenharmony_ci```js 391cb0ef41Sopenharmony_cirequire('node:inspector').console.log('a message'); 401cb0ef41Sopenharmony_ci``` 411cb0ef41Sopenharmony_ci 421cb0ef41Sopenharmony_ciThe inspector console does not have API parity with Node.js 431cb0ef41Sopenharmony_ciconsole. 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ci## `inspector.open([port[, host[, wait]]])` 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ci* `port` {number} Port to listen on for inspector connections. Optional. 481cb0ef41Sopenharmony_ci **Default:** what was specified on the CLI. 491cb0ef41Sopenharmony_ci* `host` {string} Host to listen on for inspector connections. Optional. 501cb0ef41Sopenharmony_ci **Default:** what was specified on the CLI. 511cb0ef41Sopenharmony_ci* `wait` {boolean} Block until a client has connected. Optional. 521cb0ef41Sopenharmony_ci **Default:** `false`. 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_ciActivate inspector on host and port. Equivalent to 551cb0ef41Sopenharmony_ci`node --inspect=[[host:]port]`, but can be done programmatically after node has 561cb0ef41Sopenharmony_cistarted. 571cb0ef41Sopenharmony_ci 581cb0ef41Sopenharmony_ciIf wait is `true`, will block until a client has connected to the inspect port 591cb0ef41Sopenharmony_ciand flow control has been passed to the debugger client. 601cb0ef41Sopenharmony_ci 611cb0ef41Sopenharmony_ciSee the [security warning][] regarding the `host` 621cb0ef41Sopenharmony_ciparameter usage. 631cb0ef41Sopenharmony_ci 641cb0ef41Sopenharmony_ci## `inspector.url()` 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_ci* Returns: {string|undefined} 671cb0ef41Sopenharmony_ci 681cb0ef41Sopenharmony_ciReturn the URL of the active inspector, or `undefined` if there is none. 691cb0ef41Sopenharmony_ci 701cb0ef41Sopenharmony_ci```console 711cb0ef41Sopenharmony_ci$ node --inspect -p 'inspector.url()' 721cb0ef41Sopenharmony_ciDebugger listening on ws://127.0.0.1:9229/166e272e-7a30-4d09-97ce-f1c012b43c34 731cb0ef41Sopenharmony_ciFor help, see: https://nodejs.org/en/docs/inspector 741cb0ef41Sopenharmony_ciws://127.0.0.1:9229/166e272e-7a30-4d09-97ce-f1c012b43c34 751cb0ef41Sopenharmony_ci 761cb0ef41Sopenharmony_ci$ node --inspect=localhost:3000 -p 'inspector.url()' 771cb0ef41Sopenharmony_ciDebugger listening on ws://localhost:3000/51cf8d0e-3c36-4c59-8efd-54519839e56a 781cb0ef41Sopenharmony_ciFor help, see: https://nodejs.org/en/docs/inspector 791cb0ef41Sopenharmony_ciws://localhost:3000/51cf8d0e-3c36-4c59-8efd-54519839e56a 801cb0ef41Sopenharmony_ci 811cb0ef41Sopenharmony_ci$ node -p 'inspector.url()' 821cb0ef41Sopenharmony_ciundefined 831cb0ef41Sopenharmony_ci``` 841cb0ef41Sopenharmony_ci 851cb0ef41Sopenharmony_ci## `inspector.waitForDebugger()` 861cb0ef41Sopenharmony_ci 871cb0ef41Sopenharmony_ci<!-- YAML 881cb0ef41Sopenharmony_ciadded: v12.7.0 891cb0ef41Sopenharmony_ci--> 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_ciBlocks until a client (existing or connected later) has sent 921cb0ef41Sopenharmony_ci`Runtime.runIfWaitingForDebugger` command. 931cb0ef41Sopenharmony_ci 941cb0ef41Sopenharmony_ciAn exception will be thrown if there is no active inspector. 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_ci## Class: `inspector.Session` 971cb0ef41Sopenharmony_ci 981cb0ef41Sopenharmony_ci* Extends: {EventEmitter} 991cb0ef41Sopenharmony_ci 1001cb0ef41Sopenharmony_ciThe `inspector.Session` is used for dispatching messages to the V8 inspector 1011cb0ef41Sopenharmony_ciback-end and receiving message responses and notifications. 1021cb0ef41Sopenharmony_ci 1031cb0ef41Sopenharmony_ci### `new inspector.Session()` 1041cb0ef41Sopenharmony_ci 1051cb0ef41Sopenharmony_ci<!-- YAML 1061cb0ef41Sopenharmony_ciadded: v8.0.0 1071cb0ef41Sopenharmony_ci--> 1081cb0ef41Sopenharmony_ci 1091cb0ef41Sopenharmony_ciCreate a new instance of the `inspector.Session` class. The inspector session 1101cb0ef41Sopenharmony_cineeds to be connected through [`session.connect()`][] before the messages 1111cb0ef41Sopenharmony_cican be dispatched to the inspector backend. 1121cb0ef41Sopenharmony_ci 1131cb0ef41Sopenharmony_ciWhen using `Session`, the object outputted by the console API will not be 1141cb0ef41Sopenharmony_cireleased, unless we performed manually `Runtime.DiscardConsoleEntries` 1151cb0ef41Sopenharmony_cicommand. 1161cb0ef41Sopenharmony_ci 1171cb0ef41Sopenharmony_ci### Event: `'inspectorNotification'` 1181cb0ef41Sopenharmony_ci 1191cb0ef41Sopenharmony_ci<!-- YAML 1201cb0ef41Sopenharmony_ciadded: v8.0.0 1211cb0ef41Sopenharmony_ci--> 1221cb0ef41Sopenharmony_ci 1231cb0ef41Sopenharmony_ci* {Object} The notification message object 1241cb0ef41Sopenharmony_ci 1251cb0ef41Sopenharmony_ciEmitted when any notification from the V8 Inspector is received. 1261cb0ef41Sopenharmony_ci 1271cb0ef41Sopenharmony_ci```js 1281cb0ef41Sopenharmony_cisession.on('inspectorNotification', (message) => console.log(message.method)); 1291cb0ef41Sopenharmony_ci// Debugger.paused 1301cb0ef41Sopenharmony_ci// Debugger.resumed 1311cb0ef41Sopenharmony_ci``` 1321cb0ef41Sopenharmony_ci 1331cb0ef41Sopenharmony_ciIt is also possible to subscribe only to notifications with specific method: 1341cb0ef41Sopenharmony_ci 1351cb0ef41Sopenharmony_ci### Event: `<inspector-protocol-method>`; 1361cb0ef41Sopenharmony_ci 1371cb0ef41Sopenharmony_ci<!-- YAML 1381cb0ef41Sopenharmony_ciadded: v8.0.0 1391cb0ef41Sopenharmony_ci--> 1401cb0ef41Sopenharmony_ci 1411cb0ef41Sopenharmony_ci* {Object} The notification message object 1421cb0ef41Sopenharmony_ci 1431cb0ef41Sopenharmony_ciEmitted when an inspector notification is received that has its method field set 1441cb0ef41Sopenharmony_cito the `<inspector-protocol-method>` value. 1451cb0ef41Sopenharmony_ci 1461cb0ef41Sopenharmony_ciThe following snippet installs a listener on the [`'Debugger.paused'`][] 1471cb0ef41Sopenharmony_cievent, and prints the reason for program suspension whenever program 1481cb0ef41Sopenharmony_ciexecution is suspended (through breakpoints, for example): 1491cb0ef41Sopenharmony_ci 1501cb0ef41Sopenharmony_ci```js 1511cb0ef41Sopenharmony_cisession.on('Debugger.paused', ({ params }) => { 1521cb0ef41Sopenharmony_ci console.log(params.hitBreakpoints); 1531cb0ef41Sopenharmony_ci}); 1541cb0ef41Sopenharmony_ci// [ '/the/file/that/has/the/breakpoint.js:11:0' ] 1551cb0ef41Sopenharmony_ci``` 1561cb0ef41Sopenharmony_ci 1571cb0ef41Sopenharmony_ci### `session.connect()` 1581cb0ef41Sopenharmony_ci 1591cb0ef41Sopenharmony_ci<!-- YAML 1601cb0ef41Sopenharmony_ciadded: v8.0.0 1611cb0ef41Sopenharmony_ci--> 1621cb0ef41Sopenharmony_ci 1631cb0ef41Sopenharmony_ciConnects a session to the inspector back-end. 1641cb0ef41Sopenharmony_ci 1651cb0ef41Sopenharmony_ci### `session.connectToMainThread()` 1661cb0ef41Sopenharmony_ci 1671cb0ef41Sopenharmony_ci<!-- YAML 1681cb0ef41Sopenharmony_ciadded: v12.11.0 1691cb0ef41Sopenharmony_ci--> 1701cb0ef41Sopenharmony_ci 1711cb0ef41Sopenharmony_ciConnects a session to the main thread inspector back-end. An exception will 1721cb0ef41Sopenharmony_cibe thrown if this API was not called on a Worker thread. 1731cb0ef41Sopenharmony_ci 1741cb0ef41Sopenharmony_ci### `session.disconnect()` 1751cb0ef41Sopenharmony_ci 1761cb0ef41Sopenharmony_ci<!-- YAML 1771cb0ef41Sopenharmony_ciadded: v8.0.0 1781cb0ef41Sopenharmony_ci--> 1791cb0ef41Sopenharmony_ci 1801cb0ef41Sopenharmony_ciImmediately close the session. All pending message callbacks will be called 1811cb0ef41Sopenharmony_ciwith an error. [`session.connect()`][] will need to be called to be able to send 1821cb0ef41Sopenharmony_cimessages again. Reconnected session will lose all inspector state, such as 1831cb0ef41Sopenharmony_cienabled agents or configured breakpoints. 1841cb0ef41Sopenharmony_ci 1851cb0ef41Sopenharmony_ci### `session.post(method[, params][, callback])` 1861cb0ef41Sopenharmony_ci 1871cb0ef41Sopenharmony_ci<!-- YAML 1881cb0ef41Sopenharmony_ciadded: v8.0.0 1891cb0ef41Sopenharmony_cichanges: 1901cb0ef41Sopenharmony_ci - version: v18.0.0 1911cb0ef41Sopenharmony_ci pr-url: https://github.com/nodejs/node/pull/41678 1921cb0ef41Sopenharmony_ci description: Passing an invalid callback to the `callback` argument 1931cb0ef41Sopenharmony_ci now throws `ERR_INVALID_ARG_TYPE` instead of 1941cb0ef41Sopenharmony_ci `ERR_INVALID_CALLBACK`. 1951cb0ef41Sopenharmony_ci--> 1961cb0ef41Sopenharmony_ci 1971cb0ef41Sopenharmony_ci* `method` {string} 1981cb0ef41Sopenharmony_ci* `params` {Object} 1991cb0ef41Sopenharmony_ci* `callback` {Function} 2001cb0ef41Sopenharmony_ci 2011cb0ef41Sopenharmony_ciPosts a message to the inspector back-end. `callback` will be notified when 2021cb0ef41Sopenharmony_cia response is received. `callback` is a function that accepts two optional 2031cb0ef41Sopenharmony_ciarguments: error and message-specific result. 2041cb0ef41Sopenharmony_ci 2051cb0ef41Sopenharmony_ci```js 2061cb0ef41Sopenharmony_cisession.post('Runtime.evaluate', { expression: '2 + 2' }, 2071cb0ef41Sopenharmony_ci (error, { result }) => console.log(result)); 2081cb0ef41Sopenharmony_ci// Output: { type: 'number', value: 4, description: '4' } 2091cb0ef41Sopenharmony_ci``` 2101cb0ef41Sopenharmony_ci 2111cb0ef41Sopenharmony_ciThe latest version of the V8 inspector protocol is published on the 2121cb0ef41Sopenharmony_ci[Chrome DevTools Protocol Viewer][]. 2131cb0ef41Sopenharmony_ci 2141cb0ef41Sopenharmony_ciNode.js inspector supports all the Chrome DevTools Protocol domains declared 2151cb0ef41Sopenharmony_ciby V8. Chrome DevTools Protocol domain provides an interface for interacting 2161cb0ef41Sopenharmony_ciwith one of the runtime agents used to inspect the application state and listen 2171cb0ef41Sopenharmony_cito the run-time events. 2181cb0ef41Sopenharmony_ci 2191cb0ef41Sopenharmony_ciYou can not set `reportProgress` to `true` when sending a 2201cb0ef41Sopenharmony_ci`HeapProfiler.takeHeapSnapshot` or `HeapProfiler.stopTrackingHeapObjects` 2211cb0ef41Sopenharmony_cicommand to V8. 2221cb0ef41Sopenharmony_ci 2231cb0ef41Sopenharmony_ci#### Example usage 2241cb0ef41Sopenharmony_ci 2251cb0ef41Sopenharmony_ciApart from the debugger, various V8 Profilers are available through the DevTools 2261cb0ef41Sopenharmony_ciprotocol. 2271cb0ef41Sopenharmony_ci 2281cb0ef41Sopenharmony_ci### CPU profiler 2291cb0ef41Sopenharmony_ci 2301cb0ef41Sopenharmony_ciHere's an example showing how to use the [CPU Profiler][]: 2311cb0ef41Sopenharmony_ci 2321cb0ef41Sopenharmony_ci```js 2331cb0ef41Sopenharmony_ciconst inspector = require('node:inspector'); 2341cb0ef41Sopenharmony_ciconst fs = require('node:fs'); 2351cb0ef41Sopenharmony_ciconst session = new inspector.Session(); 2361cb0ef41Sopenharmony_cisession.connect(); 2371cb0ef41Sopenharmony_ci 2381cb0ef41Sopenharmony_cisession.post('Profiler.enable', () => { 2391cb0ef41Sopenharmony_ci session.post('Profiler.start', () => { 2401cb0ef41Sopenharmony_ci // Invoke business logic under measurement here... 2411cb0ef41Sopenharmony_ci 2421cb0ef41Sopenharmony_ci // some time later... 2431cb0ef41Sopenharmony_ci session.post('Profiler.stop', (err, { profile }) => { 2441cb0ef41Sopenharmony_ci // Write profile to disk, upload, etc. 2451cb0ef41Sopenharmony_ci if (!err) { 2461cb0ef41Sopenharmony_ci fs.writeFileSync('./profile.cpuprofile', JSON.stringify(profile)); 2471cb0ef41Sopenharmony_ci } 2481cb0ef41Sopenharmony_ci }); 2491cb0ef41Sopenharmony_ci }); 2501cb0ef41Sopenharmony_ci}); 2511cb0ef41Sopenharmony_ci``` 2521cb0ef41Sopenharmony_ci 2531cb0ef41Sopenharmony_ci### Heap profiler 2541cb0ef41Sopenharmony_ci 2551cb0ef41Sopenharmony_ciHere's an example showing how to use the [Heap Profiler][]: 2561cb0ef41Sopenharmony_ci 2571cb0ef41Sopenharmony_ci```js 2581cb0ef41Sopenharmony_ciconst inspector = require('node:inspector'); 2591cb0ef41Sopenharmony_ciconst fs = require('node:fs'); 2601cb0ef41Sopenharmony_ciconst session = new inspector.Session(); 2611cb0ef41Sopenharmony_ci 2621cb0ef41Sopenharmony_ciconst fd = fs.openSync('profile.heapsnapshot', 'w'); 2631cb0ef41Sopenharmony_ci 2641cb0ef41Sopenharmony_cisession.connect(); 2651cb0ef41Sopenharmony_ci 2661cb0ef41Sopenharmony_cisession.on('HeapProfiler.addHeapSnapshotChunk', (m) => { 2671cb0ef41Sopenharmony_ci fs.writeSync(fd, m.params.chunk); 2681cb0ef41Sopenharmony_ci}); 2691cb0ef41Sopenharmony_ci 2701cb0ef41Sopenharmony_cisession.post('HeapProfiler.takeHeapSnapshot', null, (err, r) => { 2711cb0ef41Sopenharmony_ci console.log('HeapProfiler.takeHeapSnapshot done:', err, r); 2721cb0ef41Sopenharmony_ci session.disconnect(); 2731cb0ef41Sopenharmony_ci fs.closeSync(fd); 2741cb0ef41Sopenharmony_ci}); 2751cb0ef41Sopenharmony_ci``` 2761cb0ef41Sopenharmony_ci 2771cb0ef41Sopenharmony_ci[CPU Profiler]: https://chromedevtools.github.io/devtools-protocol/v8/Profiler 2781cb0ef41Sopenharmony_ci[Chrome DevTools Protocol Viewer]: https://chromedevtools.github.io/devtools-protocol/v8/ 2791cb0ef41Sopenharmony_ci[Heap Profiler]: https://chromedevtools.github.io/devtools-protocol/v8/HeapProfiler 2801cb0ef41Sopenharmony_ci[`'Debugger.paused'`]: https://chromedevtools.github.io/devtools-protocol/v8/Debugger#event-paused 2811cb0ef41Sopenharmony_ci[`session.connect()`]: #sessionconnect 2821cb0ef41Sopenharmony_ci[security warning]: cli.md#warning-binding-inspector-to-a-public-ipport-combination-is-insecure 283