node.jsv8node-inspector

Can not connect to node.js inspector using WebSocket


I am trying to develop a node.js debugger in JavaScript. I am executing a JS script using node --inspect-brk like below-

c:\node\node --inspect-brk test.js

Then it prints the WebSocket URL-

Debugger listening on ws://127.0.0.1:9229/bb6eed9d-19d4-47ae-99cf-2f2a09e125ef
For help see https://nodejs.org/en/docs/inspector

Now I am using WebSocket to connect-

var ws = new WebSocket("ws://127.0.0.1:9229/bb6eed9d-19d4-47ae-99cf-2f2a09e125ef");
ws.onmessage = function(data){console.log(data);}
ws.onerror = function(data){console.log(data);}

But, when I send the continue command then I get no response from V8-

ws.send(JSON.stringify({"seq":117,"type":"request","command":"continue"}));

I found the command for V8 protocol here - https://github.com/buggerjs/bugger-v8-client/blob/master/PROTOCOL.md

In fact ws.readyState is 1, that means connection got established properly. There is no error or message logged. Even in chrome network tab I can see data sent but there is no data received from V8, see screenshot.

Network log

Test JS code-

var x=1;
x++;
console.log(x);

Solution

  • try to send this message: {"id": 7, "method": "Runtime.runIfWaitingForDebugger"};

    i think that maybe the problem is the parameter "-brk" blocks the nodejs process. so when you send message, it can not get response. but when you open chrome to debug it at that time, you found it works well. so, open the chrome devtool and check the ws network info. i found ws sends some messages when it builds. maybe when you send message: "Runtime.runIfWaitingForDebugger", the nodejs process will continue running.

    shell.js

    ws network