setting up debugging with node and typescript in visual studio code

the problem#

Offical Solution from visual studio code homepage for setting up debugging:

{
  "name": "Launch Server",
  "program": "${workspaceFolder}/src/index.ts",
  "type": "node",
  "request": "launch",
  "skipFiles": ["<node_internals>/**"],
  "preLaunchTask": "tsc: build - tsconfig.json",
  "outFiles": ["${workspaceFolder}/build/**/*.js"]
}

and add in tsconfig.json:

{
  ...
  "sourceMap": true,
  ...
}

This works, but brings one problem to the front: Exceptions. The exception thrown will refrence to the compiled javascript code. So we have to lookup the causing source file by our selfs.

Luckily the famous ts-node package let us solve this problem with ease:

{
  "name": "Launch Server",
  "type": "node",
  "request": "launch",
  "runtimeArgs": ["-r", "ts-node/register"],
  "args": ["${workspaceFolder}/src/index.ts"]
}