调试在node.js的vm中运行的代码

时间:2022-03-17 17:10:58

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:

我有以下问题 - 我试图在使用vm模块运行的代码中调试/设置断点。我发现完成此任务的唯一方法是调用一个调试器语句:

// 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:

编辑:找到一个解决方案 - 通过将“filename”的字符串传递给vm的执行函数。例如:

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

vm.runInThisContext(codeToRun,“someFilename.js”);

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

通过这种方式,调试器“找出”代码来自何处。

1 个解决方案

#1


1  

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 ?

如您所述,node-inspector是此任务的理想选择。你不能只是启动node-debug someFilename.js并直接在UI中设置你的断点吗?

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

另一种方法是使用调试器,然后运行节点调试someFilename.js并使用命令如setBreakpoint('someFilename.js',10)

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

您可以在节点文档中找到更多信息:https://nodejs.org/api/debugger.html

#1


1  

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 ?

如您所述,node-inspector是此任务的理想选择。你不能只是启动node-debug someFilename.js并直接在UI中设置你的断点吗?

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

另一种方法是使用调试器,然后运行节点调试someFilename.js并使用命令如setBreakpoint('someFilename.js',10)

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

您可以在节点文档中找到更多信息:https://nodejs.org/api/debugger.html