Debugging mts file in VSCode (Typescript 4.5)

Viewed 183

I'm having difficulty getting breakpoints to bind when trying to debug an mts file. The code runs and outputs the correct result (in this case just a bunch of console.log statements) Interestingly, if I first go to the generated mjs file and insert a breakpoint in that file, it will then pick up break points in the mts file. If there are no breakpoints in the mjs file, the debugger doesn't attach.

Settings:

  • Node 16.13.2 or 17.3.1
  • Typescript 4.5
  • package.json: no "type" defined
  • launch.json:
    {
      "type": "node",
      "request": "launch",
      "name": "Launch Debug",
      "program": "${workspaceFolder}/lib/src/debug.mjs",
      "preLaunchTask": "tsc: build - tsconfig.debug.json"
    }
  • tsconfig.json:
    {
      "compilerOptions": {
        "target": "ES2020",
        "lib": ["DOM", "DOM.Iterable", "ES2020"],
        "allowJs": true,
        "skipLibCheck": true,
        "strict": true,
        "forceConsistentCasingInFileNames": true,
        "noEmit": true,
        "esModuleInterop": true,
        "allowSyntheticDefaultImports": true,
        "module": "ES2020",
        "moduleResolution": "node",
        "resolveJsonModule": true,
        "isolatedModules": true,
        "jsx": "preserve",
        "incremental": true,
        "typeRoots": ["./types", "./node_modules/@types"]
      },
      "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", "**/*.mts"],
      "exclude": ["node_modules", "types"]
    }
  • tsconfig.debug.json:
    {
      "extends": "./tsconfig.json",
      "compilerOptions": {
        "noEmit": false,
        "sourceMap": true,
        "outDir": "lib"
      }
    }

Has anyone been able to debug mts files without setting the package.json type setting to module or should I perhaps submit an issue to the Typescript team?

1 Answers

I figured it out! I needed to add "pauseForSourceMap": true to my launch configuration as follows:

{
   "type": "node",
   "request": "launch",
   "name": "Launch Debug",
   "program": "${workspaceFolder}/lib/src/debug.mjs",
   "preLaunchTask": "tsc: build - tsconfig.debug.json",
   "pauseForSourceMap": true
}
Related