Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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. 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).

How can I correctly debug Typescript code executed by ts-node-dev?

like image 988
303 Avatar asked Apr 13 '20 14:04

303


People also ask

How do I debug code in Visual Studio line by line?

Run to a breakpoint in code You can also select the line and then select F9, select Debug > Toggle Breakpoint, or right-click and select Breakpoint > Insert Breakpoint. The breakpoint appears as a red dot in the left margin next to the line of code. The debugger suspends execution just before the line runs.

Is it possible to debug any TypeScript file?

TypeScript is great for writing client-side code as well as Node. js applications and you can debug client-side source code with the built-in Edge and Chrome debugger.


1 Answers

Configuration for debugging in vs code with ts-node-dev to attach and launch debugger:

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:

{
  "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"]
    }
  ]
}
like image 181
HDM91 Avatar answered Nov 15 '22 08:11

HDM91