问题
I have a node_js project that includes some of our own node_js packages. They are in an npm private repo and show up in node_modules as:
@company/package_name
We are trying to set breakpoints in this code and find they are never hit.
We thought there might be a default skipFile that excludes node_modules and added to our launch.json:
"skipFiles": ["!${workspaceRoot}/node_modules/**/*.js"]
to no effect.
Any tips on how to enable debugging in the node_modules directory?
回答1:
I know it's an old question but if someone still manages to stumble upon this, you can use VS code to debug node_module files by first symlinking the node_module package with the main project and then telling VS code to use the symlink.
Symlink node_module package
If you are going to be working in a node_module package, it's a good idea to symlink it so that the changes that you make from within your projects are simultaneously applied to the module's code as well and hence, when you are done editing the package, you can directly see the diff and commit it and instead of copy-pasting it from inside node_module and applying it manually.
- To symlink a package inside node_module with your project, first clone the repo on your system.
- Run
npm link
inside the directory. - Then go to your main project and then run
npm link package_name
to link it.
For example, if you wanted to link a package sample-node-module with a project sample-project, which uses sample-node-module as its dependency, you can do it in the following manner:
cd sample-node-module
npm link
cd sample-project
npm link sample-node-module
Be careful that you enter the folder name (and not the package name itself) of the cloned repo in the second link
command. You don't have to provide the full path. You can read more about it here
Telling VS Code to use symlinks while debugging
Once you are done with above, you can simply add this small config in your launch.json of VS Code to make it detect breakpoints inside node_modules:
{
"runtimeArgs": [
"--preserve-symlinks"
]
}
Documentation about this can be found here
来源:https://stackoverflow.com/questions/47021083/vscode-debug-code-in-node-modules-directory