簡體   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