Debugging uncompiled Typescript code running on a Docker container

Using ts-node and VS Code’s built-in debugger

Mitsuhide Ohi
1 min readMay 3, 2019

I will explain how to achieve this in a very minimum setting.

Setup package.json

  • Include ts-node in devDependencies .
"devDependencies": {
...

"ts-node": "8.1.0"
}
  • Create a script running the application on ts-node with enabling Node.js inspector. Specify the opening port for debugging in here.
"scripts": {
...
"debug": "node -r ts-node/register --inspect=0.0.0.0:9229 src/app.ts"
}

Setup tsconfig.json

  • Set true to sourceMap in compilerOptions
"compilerOptions": {
...
"sourceMap": true
}

Setup debug configuration file

  • Create .vscode/launch.json . Setting the same port specified in the newly created script on package.json .
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Docker: Attach to Node",
"port": 9229,
"address": "localhost",
"sourceMapPathOverrides": {
"/app/*": "${workspaceRoot}/*"
}
}
]
}

Probably the most important setting here is sourceMapPathOverrides specifying the sourceMap path in the container. You will specify the path for WORKDIR on your Dockerfile.

"sourceMapPathOverrides": {
"<WORKDIR>/*": "${workspaceRoot}/*"
}

That’s all for the setting.

Should be possible debugging your app on VSCode.

A working example is in here.

--

--