Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging code that is run in node.js's vm

Tags:

node.js

I am having the following issue - I am attempting to debug/set a breakpoint in code that is run using the vm module. The only way that I have found to accomplish this is by putting a debugger statement:

// client code, loaded in the vm module
function printMessage() {
    debugger;
    return 56;
}

printMessage();

Is there any other way to accomplish this? Preferably using tools such as node-inspector?

EDIT: Found a solution - by passing a string for "filename" to the executing function from vm. For example:

vm.runInThisContext(codeToRun, "someFilename.js");

this way the debugger "figures out" where exactly the code came from.

like image 540
Vasil Dininski Avatar asked Jan 17 '14 10:01

Vasil Dininski


1 Answers

As you mention, node-inspector is a good candidate for this task. Can't you just start node-debug someFilename.js and set your breakpoints directly in the UI ?

Another alternative is to use debugger with and then run node debug someFilename.js and use commands like setBreakpoint('someFilename.js', 10)

You can find more information in the node documentation: https://nodejs.org/api/debugger.html

like image 183
Jspdown Avatar answered Oct 24 '22 11:10

Jspdown