home / documentation / v16 / inspector

Inspector

目录

自 v8.0.0 版本开始新增

稳定级别:2 - Stable

The node:inspector module provides an API for interacting with the V8 inspector.

It can be accessed using:

JS

M inspector.close()

历史
版本历史变更
v16.18.0The API is exposed in the worker threads.
v9.0.0自 v9.0.0 版本开始新增

Deactivate the inspector. Blocks until there are no active connections.

M inspector.console

  • Object An object to send messages to the remote inspector console.
JS

The inspector console does not have API parity with Node.js console.

M inspector.open([port[, host[, wait]]])

  • port number Port to listen on for inspector connections. Optional. Default: what was specified on the CLI.
  • host string Host to listen on for inspector connections. Optional. Default: what was specified on the CLI.
  • wait boolean Block until a client has connected. Optional. Default: false.

Activate inspector on host and port. Equivalent to node --inspect=[[host:]port], but can be done programmatically after node has started.

If wait is true, will block until a client has connected to the inspect port and flow control has been passed to the debugger client.

See the security warning regarding the host parameter usage.

M inspector.url()

Return the URL of the active inspector, or undefined if there is none.

BASH

M inspector.waitForDebugger()

自 v12.7.0 版本开始新增

Blocks until a client (existing or connected later) has sent Runtime.runIfWaitingForDebugger command.

An exception will be thrown if there is no active inspector.

C inspector.Session

The inspector.Session is used for dispatching messages to the V8 inspector back-end and receiving message responses and notifications.

M new inspector.Session()

自 v8.0.0 版本开始新增

Create a new instance of the inspector.Session class. The inspector session needs to be connected through session.connect() before the messages can be dispatched to the inspector backend.

E 'inspectorNotification'

自 v8.0.0 版本开始新增

  • Object The notification message object

Emitted when any notification from the V8 Inspector is received.

JS

It is also possible to subscribe only to notifications with specific method:

E <inspector-protocol-method>;

自 v8.0.0 版本开始新增

  • Object The notification message object

Emitted when an inspector notification is received that has its method field set to the <inspector-protocol-method> value.

The following snippet installs a listener on the 'Debugger.paused' event, and prints the reason for program suspension whenever program execution is suspended (through breakpoints, for example):

JS

M session.connect()

自 v8.0.0 版本开始新增

Connects a session to the inspector back-end.

M session.connectToMainThread()

自 v12.11.0 版本开始新增

Connects a session to the main thread inspector back-end. An exception will be thrown if this API was not called on a Worker thread.

M session.disconnect()

自 v8.0.0 版本开始新增

Immediately close the session. All pending message callbacks will be called with an error. session.connect() will need to be called to be able to send messages again. Reconnected session will lose all inspector state, such as enabled agents or configured breakpoints.

M session.post(method[, params][, callback])

自 v8.0.0 版本开始新增

Posts a message to the inspector back-end. callback will be notified when a response is received. callback is a function that accepts two optional arguments: error and message-specific result.

JS

The latest version of the V8 inspector protocol is published on the Chrome DevTools Protocol Viewer.

Node.js inspector supports all the Chrome DevTools Protocol domains declared by V8. Chrome DevTools Protocol domain provides an interface for interacting with one of the runtime agents used to inspect the application state and listen to the run-time events.

Example usage

Apart from the debugger, various V8 Profilers are available through the DevTools protocol.

CPU profiler

Here's an example showing how to use the CPU Profiler:

JS

Heap profiler

Here's an example showing how to use the Heap Profiler:

JS