简体   繁体   English

如何调试模块化的node.js应用程序,其中文件作为模块包含在内

[英]How do you debug a modularized node.js app where files are included as modules

Most of my node.js apps are written modularly using require() such that most of the actual programs execution occurs outside of the index.js file. 我的大多数node.js应用程序都是使用require()模块化编写的,因此大多数实际程序执行都在index.js文件外部进行。

I have found that when using node --inspect=0.0.0.0 index.js I am only able to set breakpoints and evaluate the program as it runs in index.js . 我发现使用node --inspect=0.0.0.0 index.js我只能设置断点并评估程序在index.js运行时的状态。

How do I setup the node.js debugger (or any other method) such that I can view specific modules that belong to my app within the debugger? 如何设置node.js调试器(或任何其他方法),以便可以在调试器中查看属于我的应用程序的特定模块?

Also I am using Visual Studio Code to debug. 另外,我正在使用Visual Studio Code进行调试。

I run all of my apps from within docker containers so I use 0.0.0.0 as the address. 我从Docker容器中运行所有应用程序,因此我使用0.0.0.0作为地址。

I have tried: 我努力了:

    node --inspect=0.0.0.0 index.js 

which connects to debugger but only sees index.js 它连接到调试器,但只能看到index.js

as well as: 以及:

    node index.js --inspect src/core/my_module.js 

which does not even start the debugger 甚至没有启动调试器

I then connect to the debugger with visual studio code and can set breakpoints within index.js. 然后,我使用Visual Studio代码连接到调试器,并可以在index.js中设置断点。 I cannot however see or set break points on any of my module files that I have in index.js such as const my_module = require('my_module') 但是,我无法在index.js中的任何模块文件上看到或设置断点,例如const my_module = require('my_module')

I am expecting that there is some way to debug a completely modularized app with node.js and Visual Studio Code. 我期望有某种方法可以使用node.js和Visual Studio Code调试完全模块化的应用程序。 How can this be done? 如何才能做到这一点?

-Thank you for your time -感谢您的时间

In VSC there is a tab in the left hand Debug column that says LOADED SCRIPTS. 在VSC中,左侧的“调试”列中有一个选项卡,显示“已加载的脚本”。

If you set a break point just below the require() statements for the modules of interest and run the program to that break point you will be able to see those files under LOADED SCRIPTS and set break points within them the same way as you would index.js 如果您在感兴趣的模块的require()语句下方设置一个断点,然后将程序运行到该断点,您将能够在LOADED SCRIPTS下查看那些文件,并以与索引相同的方式在其中设置断点.js文件

I am not sure how this can be done in Visual Studio Code, but when I debug my script in the Chrome dev tools with node --inspect-brk test.js and the execution stops on the first line, I can click on the "Add folder to workspace" button to include module folders to the file tree, open the module, set a breakpoint and continue the execution: 我不确定如何在Visual Studio Code中完成此操作,但是当我在Chrome开发工具中使用node --inspect-brk test.js调试脚本并且执行在第一行停止时,我可以点击“将文件夹添加到工作区”按钮,将模块文件夹包括到文件树中,打开模块,设置断点并继续执行:

在此处输入图片说明

Maybe there is something similar in the VS Code? VS代码中是否可能有类似内容?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM