8

Assuming we have two node.js projects with the following structure:
Projects Structure
Project Foo has Bar as a dependency by declaring the following in its package.json:

{
    "scripts": {
        "start": "ts-node src/index.ts --transpile-only --no-lazy"
    },
    "dependencies": {
        "bar": "file:../Bar"
    }
}

And I configure VSCode's debugger with the following launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "start",
            "request": "launch",
            "runtimeArgs": [
                "start"
            ],
            "runtimeExecutable": "npm",
            "smartStep": true,
            "type": "node"
        }
    ]
}

And the problem is when I step into a function imported from Bar during debugging, it'll go into a temporary compiled js source instead of the original TypeScript source, no matter I define Bar's package main entry as src/index.ts or lib/index.js. Besides, if I add breakpoints in Bar's typescript sources, they won't even be loaded.
How can I make the debugger load Bar's breakpoints, and go directly into the TypeScript sources when debugging Foo? Should I modify some properties in launch.json, or pass some more options to ts-node in the start script?
To better illustrate this problem, I've uploaded the two demo projects to GitHub: StackOverflow-Demo

starball
  • 20,030
  • 7
  • 43
  • 238
0x269
  • 688
  • 8
  • 20
  • Im pretty sure you can't set breakpoints for modules that your project loads as a dependancy, however, I don't see why you would anyways. Modules are named modules because they are intended to be modularized. The state, status, quality, structure & design of each module should be completely separate from every other module. In other words, **either the module works, or it doesn't work**, if it doesn't work then the problem should be solved by only debugging the non working module. If two modules don't work they should be fixed & debugged separately. – JΛYDΞV May 05 '22 at 14:28
  • There is one good reason to debug a module in the context of another module, and thats to test the module, which in that case, the module your testing should be added to the local folder, with any other code (which may include another module), needed to test it, but that doesn't mean to add a second `package.json` file. – JΛYDΞV May 05 '22 at 14:31
  • @jD3V Well, the real situation is that I have a database management system providing both a command-line interface and HTTP service. They share most logic in common, so I extract them to a separate core module as their dependency. And I encountered some problems in production, so I'm trying to debug in place since it's more convenient to reproduce. – 0x269 May 05 '22 at 15:03

1 Answers1

0

You should be able to do this by configuring source maps.

See the sourceMap field or the inlineSourceMap field of tsconfig. See also VS Code's guide on Debugging TypeScript.

From the VS Code docs' section on "Mapping the output location":

If generated (transpiled) JavaScript files do not live next to their source, you can help the VS Code debugger locate them by setting the outFiles attribute in the launch configuration. Whenever you set a breakpoint in the original source, VS Code tries to find the generated source by searching the files specified by glob patterns in outFiles.

starball
  • 20,030
  • 7
  • 43
  • 238