node_modules 目录下的 vscode 调试代码 [英] vscode debug code in node_modules directory

查看:111
本文介绍了node_modules 目录下的 vscode 调试代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 node_js 项目,其中包含我们自己的一些 node_js 包.它们位于 npm 私有仓库中,并在 node_modules 中显示为:

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.

我们认为可能有一个默认的 skipFile 排除了 node_modules 并添加到我们的 launch.json 中:

We thought there might be a default skipFile that excludes node_modules and added to our launch.json:

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

没有效果.

关于如何在 node_modules 目录中启用调试的任何提示?

Any tips on how to enable debugging in the node_modules directory?

推荐答案

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

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.

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

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

例如,如果您想将包 sample-node-module 与使用 sample-node-module<的项目 sample-project 链接起来/em> 作为它的依赖,您可以通过以下方式进行:

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

请注意在第二个 link 命令中输入克隆存储库的文件夹名称(而不是包名称本身).您不必提供完整路径.您可以在此处

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

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

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

这篇关于node_modules 目录下的 vscode 调试代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆