5

I have made a library in Typescript and will be using it in other NodeJS projects. I have included the source code in the NPM package, so when I install it in my projects where I consume this package, I get the source as well in the node_modules folder.

Now, while debugging my project, I want to be able to hit the breakpoints set in the library code inside node_modules. How can I do this?

Suppose I have my project that I am trying to debug at root/my-project and this project is using an NPM package named @my-org/common which is located at root/my-org-common. Since this library is installed in my-project using NPM, the code for @my-org/common is also included in node_modules at root/my-project/node_modules. How do I set up my VSCode so that breakpoints inside code at root/my-project/node_modules are hit?

Ahmad Habib
  • 2,053
  • 1
  • 13
  • 28
Stupid Man
  • 885
  • 2
  • 14
  • 31
  • If you want to debug the library, maybe just temporarily adjust the `main` property in package.json to point to `src` (or equivalent) instead of `dist`/`build`? – Siddharth May 19 '21 at 12:41
  • The project code I am talking about is an ExpressJS project which has routers and controllers and the library code has just data models, utils and service methods. I want to debug the library methods which are called through the service. Will changing the `main` in package.json work in this case? – Stupid Man May 19 '21 at 12:54
  • Unfortunately there are no tests for the library code or I would have debugged it using those tests. – Stupid Man May 19 '21 at 12:55

1 Answers1

3

Found a solution here: vscode debug code in node_modules directory

Basically, you have to create a symlink which points to the code of the library and then instruct VSCode to use this symlink by putting this in launch.json:

{
    "runtimeArgs": [
        "--preserve-symlinks"
    ]
}
Stupid Man
  • 885
  • 2
  • 14
  • 31