简体   繁体   中英

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.

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 .

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?

Also I am using Visual Studio Code to debug.

I run all of my apps from within docker containers so I use 0.0.0.0 as the address.

I have tried:

    node --inspect=0.0.0.0 index.js 

which connects to debugger but only sees 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. 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')

I am expecting that there is some way to debug a completely modularized app with node.js and 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.

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

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:

在此处输入图片说明

Maybe there is something similar in the VS Code?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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