简体   繁体   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

I have a Typescript project that is launched as follows:我有一个 Typescript 项目,启动如下:

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

I can debug it with Visual Studio Code, but the debugger breaks at the wrong lines.我可以使用 Visual Studio Code 对其进行调试,但调试器在错误的行处中断。 The only reasonable explanation I can think of, is that ts-node-dev does not point the debugger to the source maps (which are there).我能想到的唯一合理的解释是 ts-node-dev 没有将调试器指向源映射(它们在那里)。

How can I correctly debug Typescript code executed by ts-node-dev?如何正确调试 ts-node-dev 执行的 Typescript 代码?

Configuration for debugging in vs code with ts-node-dev to attach and launch debugger:使用 ts-node-dev 在 vs 代码中调试以附加和启动调试器的配置:

package.json: 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",
  }
}

launch.json:发射.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"]
    }
  ]
}

I had the same question, which brought me to this (old) post.我有同样的问题,这把我带到了这个(旧)帖子。 I found the solution to my problem at https://gist.github.com/cecilemuller/2963155d0f249c1544289b78a1cdd695 so am posting it here in case anyone else finds themselves here!我在https://gist.github.com/cecilemuller/2963155d0f249c1544289b78a1cdd695找到了我的问题的解决方案,以防其他人在这里找到!

This VS Code configuration allowed me to stop at breakpoints on the correct lines in my TypeScript code:此 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/**"]
    }
  ]
}

This is what worked for me.这对我有用。 attach type debugger was not working for me but launch type is working fine. attach类型调试器对我不起作用,但launch类型工作正常。 Breakpoints works as well even though sometimes it goes to the ts-node-dev source files and I guess we can't do anything about it.即使断点有时会转到ts-node-dev源文件,但我想我们对此无能为力。

It runs tsnd which is just the alias for 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>/**"
            ],
        },
}

"program" could be changed to run a specific file by replacing ${file} with that filename but with the above config, it will run the opened file with the debugger.可以通过将${file}替换为该文件名来更改"program"以运行特定文件,但使用上述配置,它将使用调试器运行打开的文件。

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

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