'use strict';
const CDP = require('chrome-remote-interface');
const cp = require('child_process');
const sleep = require('mz-modules/sleep');
async function fn() {
let client;
try {
const child = cp.fork('./child', [], { execArgv: [ '--inspect-brk=9229' ] });
await sleep('2s');
console.log(child.connected && 'fork child');
client = await CDP({ port: 9229 });
const { Debugger } = client;
console.log('attached');
// pending at here
await Debugger.enable();
console.log('enabled');
await Debugger.resume();
console.log('resume');
} catch (err) {
console.error(err);
} finally {
console.log('close');
if (client) {
await client.close();
}
}
}
fn().catch(console.error);
child.js
console.log(1);
console.log(2);
output:
➜ cdp-test node index.js
Debugger listening on ws://127.0.0.1:9229/d23439a7-9607-43f2-93f3-d47e09dcd436
For help see https://nodejs.org/en/docs/inspector
fork child
attached
| Component | Version
|-|-
| Operating system | Mac
| Node.js | v8.5.0
| chrome-remote-interface | latest ^0.25.2
Is Chrome running in a container? NO
You need to issue Runtime.runIfWaitingForDebugger before enabling the Debugger domain (see this wiki):
await Runtime.runIfWaitingForDebugger();
await Debugger.enable();
But after that you'll get Error: Can only perform operation while paused meaning that you should explicitly wait the debugger to be paused in order to resume it:
await Debugger.paused();
await Debugger.resume();
Hope it helps. Unfortunately this part is a bit contrived and scarcely documented. I suggest you to inspect DevTools (see "Sniffing the protocol") to figure out how things are implemented.
thanks
Most helpful comment
You need to issue
Runtime.runIfWaitingForDebuggerbefore enabling theDebuggerdomain (see this wiki):But after that you'll get
Error: Can only perform operation while pausedmeaning that you should explicitly wait the debugger to be paused in order to resume it:Hope it helps. Unfortunately this part is a bit contrived and scarcely documented. I suggest you to inspect DevTools (see "Sniffing the protocol") to figure out how things are implemented.