繁体   English   中英

如何使用 ts-node-dev 和正确的行号在 Visual Studio Code 中调试 Typescript 代码

[英]How to debug Typescript code in Visual Studio Code with ts-node-dev and correct line numbers

我有一个 Typescript 项目,启动如下:

ts-node-dev  --preserve-symlinks --inspect=0.0.0.0  -- src/server.ts

我可以使用 Visual Studio Code 对其进行调试,但调试器在错误的行处中断。 我能想到的唯一合理的解释是 ts-node-dev 没有将调试器指向源映射(它们在那里)。

如何正确调试 ts-node-dev 执行的 Typescript 代码?

使用 ts-node-dev 在 vs 代码中调试以附加和启动调试器的配置:

package.json:

{
  "scripts": {
    "dev:debug": "ts-node-dev --transpile-only --respawn --inspect=4321 --project tsconfig.dev.json src/server.ts",
    "dev": "ts-node-dev --transpile-only --respawn --project tsconfig.dev.json src/server.ts",
  }
}

发射.json:

{
  "version": "0.1.0",
  "configurations": [
    {
      "type": "node",
      "request": "attach",
      "name": "Attach to dev:debug",
      "protocol": "inspector",
      "port": 4321,
      "restart": true,
      "cwd": "${workspaceRoot}"
    },
    {
      "type": "node",
      "request": "launch",
      "name": "Debug",
      "protocol": "inspector",
      "cwd": "${workspaceRoot}",
      "runtimeExecutable": "npm",
      "runtimeArgs": ["run-script", "dev"]
    }
  ]
}

我有同样的问题,这把我带到了这个(旧)帖子。 我在https://gist.github.com/cecilemuller/2963155d0f249c1544289b78a1cdd695找到了我的问题的解决方案,以防其他人在这里找到!

此 VS 代码配置允许我在 TypeScript 代码中正确行的断点处停止:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Example",
      "type": "node",
      "request": "launch",
      "runtimeExecutable": "node",
      "runtimeArgs": ["--nolazy", "-r", "ts-node/register/transpile-only"],

      "args": ["src/script.ts", "--example", "hello"],
      
      "cwd": "${workspaceRoot}",
      "internalConsoleOptions": "openOnSessionStart",
      "skipFiles": ["<node_internals>/**", "node_modules/**"]
    }
  ]
}

这对我有用。 attach类型调试器对我不起作用,但launch类型工作正常。 即使断点有时会转到ts-node-dev源文件,但我想我们对此无能为力。

它运行 tsnd,它只是 ts-node-dev 的别名。

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "pwa-node",
            "name": "launch",
            "request": "launch",
            "runtimeExecutable": "${workspaceFolder}/node_modules/.bin/tsnd",
            "program": "${file}",
            "args": [
                "--transpile-only",
                "--respawn",
                "--project",
                "tsconfig.dev.json",
            ],
            "skipFiles": [
                "<node_internals>/**"
            ],
        },
}

可以通过将${file}替换为该文件名来更改"program"以运行特定文件,但使用上述配置,它将使用调试器运行打开的文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM