The inspector module provides an API that makes it easy to interact with the V8 inspector.
One can easily open a inspector like:
constinspector=require("inspector");inspector.open();// <- inspector.open([port[, host[, wait]]])/*Debugger listening on ws://127.0.0.1:9229/ab01a630-90ad-4c87-91c9-a665632c1d8bFor help see https://nodejs.org/en/docs/inspector*/inspector.url();// 'ws://127.0.0.1:9229/ab01a630-90ad-4c87-91c9-a665632c1d8b'inspector.console.log("This should be visible on the debugger console");
But, most of times we would want to wait until the debugger is connected to our process, that's where inspector.waitForDebugger() will be useful, here is a quick example.
>nodefoo.js^Thiswouldmaketheprocesswaittillthedebuggerisconnected./* Debugger listening on ws://127.0.0.1:9229/b4b0e48d-2e4c-4cde-8177-6924c662aef4 For help, see: https://nodejs.org/en/docs/inspector Debugger attached. Waiting for the debugger to disconnect... */
Dispatching and receiving message responses and notifications is possible with the session API:
session.on(<inspector-protocol-method>) where the methods are from devtools-protocol.
P.S: This is an experimental API with Stability of 1 meaning is not subject to Semantic Versioning rules. Non-backward compatible changes or removal may occur in any future release. Use of the feature is not recommended in production environments.