36

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?

R. Richards
  • 24,603
  • 10
  • 64
  • 64
Chris Kinsman
  • 461
  • 1
  • 4
  • 5

2 Answers2

38

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.

  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.

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

The Doctor
  • 1,302
  • 12
  • 23
  • 3
    Hey Chris, if this answer solves your problem, please upvote and mark the answer as 'Accepted answer'. Thanks :) – The Doctor Jul 22 '19 at 07:03
  • 1
    surely there must be a better way? I'm reading about `debug.javascript.autoAttachSmartPattern` .. havnt figured it out yet, but want to use the configurable way if possible rather than playing with symlinks – Flion Nov 12 '20 at 05:21
  • 3
    Hey @Flion thanks for your response and pointing the new method out! As you can see, this answer itself is more than 2 years old and at that time this is what worked for me. I looked at what you have suggested and seems like it might work but since I can't try out what you have suggested right now, if you can write another answer or even update this one to include that info would be great :) – The Doctor Nov 15 '20 at 11:45
  • It's not working please [advice](https://stackoverflow.com/questions/69136755/cant-set-breakpoints-on-npm-linked-library) – Kid Sep 14 '21 at 17:34
  • I tried to see if this helps with my linked library but it's not working. Might be a path issue as well? Trying to make sense on what to set for the correct path in sourceMapPathOverrides for the linked module. – spankmaster79 Jun 03 '22 at 08:48
  • if you are using `yarn`, it's the same, just `yarn` instead of `npm` – Reza Bayat Aug 04 '22 at 12:44
0

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:

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