vscode debug code in node_modules directory

前端 未结 1 741
感动是毒
感动是毒 2021-02-12 23:01

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
<         


        
相关标签:
1条回答
  • 2021-02-12 23:43

    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

    0 讨论(0)
提交回复
热议问题