在VSCode中调试MTS文件(TypeScript 4.5)。

5

在尝试调试mts文件时,我遇到了断点无法绑定的困难。代码可以运行并输出正确的结果(在这种情况下只是一堆console.log语句)

有趣的是,如果我先进入生成的mjs文件并在该文件中插入断点,它将接着捕捉到mts文件中的断点。如果mjs文件中没有断点,则调试器不会附加。

设置:

  • Node 16.13.2或17.3.1
  • Typescript 4.5
  • package.json:未定义“type”
  • 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"
      }
    }

有人能够在不将 package.json 中的 type 设置为 module 的情况下调试 mts 文件吗?或者我应该向 TypeScript 团队提交一个问题吗?


您的配置无法如所示,因为它包含重复的关键字 noEmit - Aluan Haddad
1
@AluanHaddad 是的,你说得对。我试图通过手动合并配置文件来回答这个问题,但结果适得其反!我已经更正了,现在使用的是正确的配置文件。 - sigma54
1个回答

1
我想通了!我需要将 "pauseForSourceMap": true 添加到我的启动配置中,如下所示:
{
   "type": "node",
   "request": "launch",
   "name": "Launch Debug",
   "program": "${workspaceFolder}/lib/src/debug.mjs",
   "preLaunchTask": "tsc: build - tsconfig.debug.json",
   "pauseForSourceMap": true
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接