简体   繁体   English

node_modules 目录下的 vscode 调试代码

[英]vscode debug code in node_modules directory

I have a node_js project that includes some of our own node_js packages.我有一个 node_js 项目,其中包含一些我们自己的 node_js 包。 They are in an npm private repo and show up in node_modules as:它们位于 npm 私有仓库中,并在 node_modules 中显示为:

@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:我们认为可能有一个默认的 skipFile 排除了 node_modules 并添加到我们的 launch.json 中:

"skipFiles": ["!${workspaceRoot}/node_modules/**/*.js"]

to no effect.没有效果。

Any tips on how to enable debugging in the node_modules directory?关于如何在 node_modules 目录中启用调试的任何提示?

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.我知道这是一个老问题,但如果有人仍然设法偶然发现这一点,您可以使用 VS 代码调试 node_module 文件,方法是首先将 node_module 包与主项目进行符号链接,然后告诉 VS 代码使用符号链接。

Symlink node_module package符号链接 node_module 包

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.如果您打算在 node_module 包中工作,最好对其进行符号链接,以便您在项目中所做的更改也同时应用于模块的代码,因此,当您完成编辑包时,您可以直接查看差异并提交它,而不是从 node_module 内部复制粘贴它并手动应用它。

  1. To symlink a package inside node_module with your project, first clone the repo on your system.要将 node_module 中的包与您的项目进行符号链接,请首先在您的系统上克隆存储库。
  2. Run npm link inside the directory.在目录中运行npm link
  3. Then go to your main project and then run npm link package_name to link it.然后转到您的主项目,然后运行npm link package_name来链接它。

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:例如,如果您想将包sample-node-module与使用sample-node-module作为其依赖项的项目sample-project链接,您可以通过以下方式进行:

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.请注意,在第二个link命令中输入克隆存储库的文件夹名称(而不是包名称本身)。 You don't have to provide the full path.您不必提供完整路径。 You can read more about it here你可以在这里阅读更多关于它的信息

Telling VS Code to use symlinks while debugging告诉 VS Code 在调试时使用符号链接

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:完成上述操作后,您可以简单地在 VS Code 的 launch.json 中添加这个小配置,以使其检测 node_modules 中的断点:

{
"runtimeArgs": [
    "--preserve-symlinks"
]
}

Documentation about this can be found here可以在此处找到有关此的文档

The problem can be with source maps.问题可能出在源地图上。 Try to add node_modules/example-package files to resolveSourceMapLocations in launch.json, where example-package is directory of module, in which you want to set breakpoint:尝试在 launch.json 中的resolveSourceMapLocations中添加node_modules/example-package文件,其中example-package是模块的目录,要在其中设置断点:

"resolveSourceMapLocations": [
    "${workspaceFolder}/**",
    "!**/node_modules/**",
    "node_modules/example-package/**/*.js",
]

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

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