This project has moved. For the latest updates, please go here.

Debugging with node-inspector

Oct 5, 2015 at 4:20 PM
There is a node.js package called node-inspector to debug node applications.

https://www.npmjs.com/package/node-inspector

I tried using it to debug my V8 scripts on ClearScript but could not succeed.

I start node-inspector using the following command:

node-inspector -d 9222

I initialize my V8 engine with the following VB code:

Dim engine As V8ScriptEngine = New V8ScriptEngine("global", V8ScriptEngineFlags.EnableDebugging, 9222)

I have "debugger;" statement in my Javascript source code.

However when I execute the script it does not break at "debugger" statements.

Can you please test this application for debugging ClearScript?
Oct 6, 2015 at 12:35 PM
Edited Oct 6, 2015 at 12:40 PM
After long hours of googling and testing I did succeed debugging ClearScript code using node-inspector.

First of all, the latest version (0.12.3) has many extensions for debugging node.js scripts so I used an older version (0.5.0) which has less liability to node.js.

This is advised by the author of node-inspector in the following stackoverflow thread:

http://stackoverflow.com/questions/22153131/debug-v8-with-node-inspector

I used the following command to install v0.5.0.
npm install node-inspector@0.5.0
You must have node.js (https://nodejs.org/en/) installed on your computer to run this command. Execute it in Node.js command prompt.

It is installed in folder "%USERPROFILE%\node_modules" by default.

I added one line of code to %USERPROFILE%\node_modules\node-inspector\lib\PageAgent.js:

getResourceTree: function(params, done) {
return; //return empty tree
...

This completes the installation.

Here is the guide to debug a javascript file executed by ClearScript:

Open a command prompt and execute the following command:

%USERPROFILE%\node_modules\.bin\node-inspector.cmd

You should see the following lines if node-inspector is running successfully.

Node Inspector v0.5.0
info - socket.io started
Visit http://127.0.0.1:8080/debug?port=5858 to start debugging.

Initialize your V8 engine as in the following code line (VB):

Dim engine As New V8ScriptEngine("global", V8ScriptEngineFlags.EnableDebugging, 9222)

Put a breakpoint right after this line in your code and run your application to reach this breakpoint.

Make sure you have "debugger;" statement in the first line of your JavaScript code.

Open a Chrome browser and navigate to the following address:

http://127.0.0.1:8080/debug?port=9222

Press "Continue Debugging" button in Vİsual Studio toolbar.

Now you should see your script code stopped at the first "debugger;" line in Chrome browser.

You can continue debugging from here.

Write in this thread if you have any questions.
Coordinator
Oct 6, 2015 at 1:22 PM
Hello ahmetuzun,

Based on a brief investigation, it looks like node-inspector expects to find certain node.js variables in the script environment. Here's one of the first messages ClearScript receives:
{"seq":12,"type":"request","command":"evaluate","arguments":{"expression":"JSON.stringify((function () {\n    return {\n      pid: process.pid,\n      cwd: process.cwd(),\n      filename: process.mainModule ? process.mainModule.filename : process.argv[1],\n      nodeVersion: process.version\n    };\n  })()).match(/.{1,80}/g).slice()","global":true,"maxStringLength":-1}}
And here's V8's response:
{"seq":3,"request_seq":12,"type":"response","command"
:"evaluate","success":false,"message":"ReferenceError: process is not defined","
running":false}
This appears to confuse node-inspector, as ClearScript receives no further messages. By the way, the tested and recommended V8 debugger for ClearScript is Eclipse with Google Chrome Developer Tools. More information is available in the ReadMe.

Update: Ah, it looks like you've already found a solution. Thank you very much for posting it!

Cheers!
Oct 7, 2015 at 4:23 PM
Edited Oct 7, 2015 at 4:24 PM
Here is the video of the node-inspector debugger embedded in a C# application.

The application uses the following libraries:

CefSharp to embed Chrome browser in the application (https://github.com/cefsharp/CefSharp/tree/CefSharp1)

ClearScript to execute JavaScript code.

node-inspector (node.js application) + embedded Chrome browser to debug executed code.

https://youtu.be/rGgfJxEWGds