如何使用节点检查器正确调试 node.js?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8882927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:01:44  来源:igfitidea点击:

How to PROPERLY debug node.js with node inspector?

debuggingnode.js

提问by Totty.js

I have an app built in node.js and I use the node inspector in order to debug. But it's quite hard because of this:

我有一个内置于 node.js 的应用程序,我使用节点检查器进行调试。但这很难,因为:

  1. My breakpoints are never saved after I restart the server
  2. I cannot put a breakpoint on a file that has not loaded yet; so I have to step into from the first script to the one I want; REALLY PAINFULL!
  1. 重新启动服务器后,我的断点永远不会保存
  2. 我无法在尚未加载的文件上设置断点;所以我必须从第一个脚本进入我想要的脚本;真的很痛苦!

How do you really debug node.js with node inspector?

你是如何使用节点检查器真正调试 node.js 的?

The videos on how to use node.js are quite misleading as everything is into a module...
http://www.youtube.com/watch?v=AOnK3NVnxL8

关于如何使用 node.js 的视频非常具有误导性,因为一切都在一个模块中......
http://www.youtube.com/watch?v=AOnK3NVnxL8

or this one the scripts appear are already loaded in the first script
http://www.youtube.com/watch?v=HJOH0-g8f6E&feature=mfu_in_order&list=UL

或者出现的这个脚本已经加载到第一个脚本中
http://www.youtube.com/watch?v=HJOH0-g8f6E&feature=mfu_in_order&list=UL

Edit:

编辑:

Nobody can answer this question? :s

这个问题没人能回答吗?:s

采纳答案by Jauco

In javascript you can set breakpoints using the debugger;statement. However, they will only pause node if a debugger is actually attached.

在 javascript 中,您可以使用该debugger;语句设置断点。但是,如果实际连接了调试器,它们只会暂停节点。

So launch your node script using

因此,使用启动您的节点脚本

node --debug-brk myfile.js

then launch node-inspector and press the play button to continue to the next breakpoint and it will hit your debugger;breakpoint (at least that works for me ATM)

然后启动节点检查器并按播放按钮继续下一个断点,它将击中您的debugger;断点(至少对我来说是 ATM)

(as noted in the comments: in recent versions of node you no longer have to separately install node-inspector. If you launch node using node --debug-brk --inspect myfile.jsyou get a url that launches the debugger in your browser).

(如评论中所述:在最新版本的 node 中,您不再需要单独安装 node-inspector。如果您使用 nodenode --debug-brk --inspect myfile.js启动,您将获得一个在浏览器中启动调试器的 URL)。

you still need one extra click after restarting, but at least your breakpoints are saved.

重新启动后您仍然需要额外单击一次,但至少您的断点已保存。

if your breakpoint is not hit automatically, but only after some user action you don't need the --debug-brkof course.

如果您的断点没有自动命中,但只有在某些用户操作之后,您--debug-brk当然不需要。

回答by mtsr

The problem with client-side breakpoints is that it's hard to keep track of the breakpoint position when the file changes. Unlike in an editor, it cannot keep track of lines being changed, etc.

客户端断点的问题在于当文件更改时很难跟踪断点位置。与编辑器不同,它无法跟踪更改的行等。

@RyanOlds suggestion of using debugger; statements is also a good one, but you have to make sure the debugger is connected before the statement is evaluated, because it is ignored otherwise. Starting with --debug-brk is a good way to force this, because the execution is paused on the first line allowing you to attach the debugger and then continue the execution.

@RyanOlds 建议使用调试器;语句也是一个不错的选择,但您必须确保在评估语句之前调试器已连接,否则会被忽略。以 --debug-brk 开头是强制执行此操作的好方法,因为执行在第一行暂停,允许您附加调试器然后继续执行。

You could try debugging with node's internal debugger.

您可以尝试使用 node 的内部调试器进行调试

Edit: However, according to the v8 DebuggerProtocolit's possible to set breakpoints on script that hasn't been loaded yet AND you can set breakpoints by function, script and more. It should therefore be possible for node-inspector to keep track of your breakpoints (in a session, or whatever). It doesn't do so right now, though.

编辑:但是,根据v8 DebuggerProtocol,可以在尚未加载的脚本上设置断点,并且您可以按函数、脚本等设置断点。因此,节点检查器应该可以跟踪您的断点(在会话中,或其他)。不过,它现在不这样做。

Maybe if v8 allows a certain piece of code to trigger a breakpoint, similar to nodes debugger? Edit: It does, you should be able to trigger a break by throwing any old exception (caught or uncaught).

也许如果 v8 允许某段代码触发断点,类似于节点调试器?编辑:确实如此,您应该能够通过抛出任何旧异常(捕获或未捕获)来触发中断。

回答by alehro

The new version (0.3.x) of node inspector saves breakpoints in browser's local storage and restores them automatically.

节点检查器的新版本(0.3.x)将断点保存在浏览器的本地存储中并自动恢复它们。

https://github.com/node-inspector/node-inspector/pull/116

https://github.com/node-inspector/node-inspector/pull/116

回答by asparagino

Try using IntelliJ WebStorm - there's a free trial and licenses aren't outrageously expensive. It lets you save breakpoints in all your files prior to starting up its own internal node process and remembers them across process restarts.

尝试使用 IntelliJ WebStorm - 有一个免费试用版,而且许可证也不会太贵。它允许您在启动自己的内部节点进程之前在所有文件中保存断点,并在进程重新启动时记住它们。

I agree - node-inspector looks brilliant, but is quite useless unless your app has a clear place to set a breakpoint in the top level script just after your source files have loaded, but before you hit the area you want to debug. You can structure your own code this way, but you won't be so lucky with other helpful libraries you want to include. Also... why should a debugging tool dictate your project structure!

我同意 - node-inspector 看起来很棒,但它非常无用,除非您的应用程序在源文件加载后但在到达要调试的区域之前有一个明确的位置可以在顶级脚本中设置断点。您可以通过这种方式构建您自己的代码,但是如果您想包含其他有用的库,就不会那么幸运了。另外...为什么要使用调试工具来决定您的项目结构!

Forgetting breakpoints is extremely unhelpful... most of my debug runs take more than one walkthrough, as in other people's code it's easy to step past where you want to be.

忘记断点是非常无益的......我的大多数调试运行都需要不止一次演练,因为在其他人的代码中很容易跳过你想要的地方。

回答by Silviu-Marian

You can use node-codein for inspection. It won't do runtime breakpoints but it should ease the inspection process.

您可以使用 node-codein 进行检查。它不会做运行时断点,但它应该简化检查过程。

https://github.com/ketamynx/node-codein/

https://github.com/ketamynx/node-codein/

回答by willko747

Also worth noting.. vscode has a great debugger for node.

同样值得注意的是.. vscode 有一个很好的 node 调试器。

https://code.visualstudio.com/

https://code.visualstudio.com/

Available on Mac, Linux, & Windows.

适用于 Mac、Linux 和 Windows。

It does runtime breakpoints (without the need of writing debugger; statements), supports variable watches, and even has a call stack window (very nice).

它执行运行时断点(无需编写调试器;语句),支持变量监视,甚至还有一个调用堆栈窗口(非常好)。

Everything is so automated, it is now my goto over sublime text when using nodejs (and I LOVE sublime).

一切都如此自动化,现在我在使用 nodejs 时会转到 sublime 文本(我喜欢 sublime)。

回答by RoccoB

This is built in now including saving breakpoints. I just tested it in node 7.3.0.

这是现在内置的,包括保存断点。我刚刚在节点 7.3.0 中对其进行了测试。

node --inspect --debug-brk app.js

node --inspect --debug-brk app.js

This prints a url like this

这会打印这样的网址

To start debugging, open the following URL in Chrome: chrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:9229/c3d5d93e-9d27-41b9-a4da-607e43c9d4f8

To start debugging, open the following URL in Chrome: chrome-devtools://devtools/bundled/inspector.html?experiments=true&v8only=true&ws=127.0.0.1:9229/c3d5d93e-9d27-41b9-a4da-607e43c9d4f8

Put that in Chrome and you're good to go.

把它放在 Chrome 中,你就可以开始了。

If you want to skip copy/pasting the url, do this:

如果您想跳过复制/粘贴网址,请执行以下操作:

npm install -g inspect-process inspect --debug-brk app.js

npm install -g inspect-process inspect --debug-brk app.js

Unfortunately the inspect-processmethod doesn't retain the breakpoints :-(.

不幸的是,该inspect-process方法不保留断点:-(。

Here's a video I made: https://youtu.be/rtZKUnks6jI

这是我制作的视频:https: //youtu.be/rtZKUnks6jI