javascript 如何使用 Visual Studio 2013 和 Node Tools NTVS 调试 node.js 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26554093/
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-10-28 06:08:17  来源:igfitidea点击:

How to debug node.js application with Visual Studio 2013 and Node Tools NTVS

javascriptnode.jsdebuggingvisual-studio-2013ntvs

提问by Andreas Reiff

I have moved an existing node.js + express project to VS because I prefer the IDE over JetBrains for now (used VS for years, only peeked into Webstorm).

我已经将现有的 node.js + express 项目移到了 VS,因为我现在更喜欢 IDE 而不是 JetBrains(使用 VS 多年,只看到了 Webstorm)。

I used NTVS new project->from existing sources and all files were imported successfully.

我使用了 NTVS 新项目->来自现有源,并且所有文件都已成功导入。

Afterwards, I opened the project settings of my project and set the node.exe arguments to bin\www, startup file for express.

之后,我打开我的项目的项目设置,将 node.exe 参数设置为 bin\www,express 的启动文件。

When I press F5 (debug) I get the console.log messages I have put into the www and app.js files in the opening command prompt, and it looks like the server is running (cannot confirm, I want to debug if everything is working), but the VS debugger directly exits again, it also does not open any page in the browser I selected for debugging.

当我按 F5(调试)时,我在打开的命令提示符中收到我放入 www 和 app.js 文件的 console.log 消息,看起来服务器正在运行(无法确认,如果一切正常,我想调试工作),但VS调试器直接再次退出,它也没有在我选择调试的浏览器中打开任何页面。

My node app actually is a REST webservice, so I want to test different URLs with different parameters.

我的节点应用程序实际上是一个 REST 网络服务,所以我想用不同的参数测试不同的 URL。

Also, I cannot access the app on the port I specified, though when I directly start it from node.exe I can, even though the command prompt is still open.

此外,我无法访问我指定的端口上的应用程序,但当我直接从 node.exe 启动它时,即使命令提示符仍处于打开状态。

(I have NTVS and WebEssentials installed - some operations take a long long time, but I attribute this to NTVS being still an early version.)

(我安装了 NTVS 和 WebEssentials - 有些操作需要很长时间,但我将此归因于 NTVS 仍然是早期版本。)

Question: how does the Visual Studio debugger stay connected to the node.js application so I can use breakpoints and use any browser then to connect and test different URLs?(Even a breakpoint put on the console.log that gets printed during startup is not being triggered.)

问题:Visual Studio 调试器如何与 node.js 应用程序保持连接,以便我可以使用断点并使用任何浏览器连接和测试不同的 URL?(即使在启动期间打印的 console.log 上的断点也不会被触发。)

回答by Andreas Reiff

For everyone who asks receives, and the one who searches finds....

因为凡祈求的就得着,寻找的就寻见……

(and yes, I did spend a long time searching and trying before posting here..)

(是的,在发帖之前,我确实花了很长时间搜索和尝试……)

Settings

设置

Kind of nice to debug node.js server with VS..

用 VS 调试 node.js 服务器很不错..

hope this helps someone

希望这有助于某人

Edit: The arguments to node.exe can be hard to read in the image. It must be

编辑: node.exe 的参数可能很难在图像中读取。一定是

 --debug=<portno>

that is with two dashes (and not just one) to specify the debug port.

那就是用两个破折号(不仅仅是一个)来指定调试端口。

回答by onelaview

Not so much knowledge on expressjsbut with a recent release of NTVS1.0 Alpha, I did find it supports remote debugging which can be also used to debug nodejsapp running locally - anyway haven't tried if it works with nodejs app + expressjs but it should.

关于expressjs最近发布的NTVS1.0 Alpha 的知识不多,我确实发现它支持远程调试,也可以用来调试nodejs本地运行的应用程序 - 无论如何还没有尝试过它是否可以与 nodejs app + expressjs 一起使用,但应该可以。

I followed the step in this video https://youtu.be/-ir9ZB8lUg4which is

我按照此视频https://youtu.be/-ir9ZB8lUg4 中的步骤操作

  1. Run your nodejs with node.exe RemoteDebug.js <your_javascript_file>.
    RemoteDebug.jshas come when you install NTVS.
  2. In Visual Studio, select Debug> Attach to Process
  3. Select Node.js remote debuggingfor Transport
  4. Enter localhost:5859for Qualifier
  5. Click Attach
  1. 使用node.exe RemoteDebug.js <your_javascript_file>.
    RemoteDebug.js安装 NTVS 时出现了。
  2. 在 Visual Studio 中,选择Debug>Attach to Process
  3. 选择Node.js remote debugging运输
  4. 进入localhost:5859资格赛
  5. 点击 Attach

This will put Visual Studio in debugging mode which you can set a breakpoint, do step-in/step-out, very same experience when you use VS to debug .NET app.

这将使 Visual Studio 处于调试模式,您可以在该模式下设置断点、执行单步执行/单步执行,这与您使用 VS 调试 .NET 应用程序时的体验完全相同。

回答by Vignesh Subramanian

Its pretty straight forward with NTVS, you can download required version for your windows from github here

它与NTVS非常直接,您可以从这里的 github下载 Windows 所需的版本

Once you install NTVS, NodeJS project templates will be added

安装 NTVS 后,将添加 NodeJS 项目模板

Now, Goto File->New project -> Basic NodeJS Express 3 application (it will be available in javascript project templates)

现在,转到 File->New project -> Basic NodeJS Express 3 应用程序(它将在 javascript 项目模板中可用

Now just goto debug and select Start Debugging, add breakpoints where ever required and you can start debugging

现在只需转到 debug 并选择Start Debugging,在需要的地方添加断点,您就可以开始调试了