Complete Guide on Debugging Typescript Node Application in VS Code Using Remote Development Feature
Tags: typescript, vscode, node, debugging
Published: 2022-01-11 04:55:42


This is simple guide to get started with debugging typescript node application in VS Code using the Remote Development extension. The issue with this use-case is that the application files are mounted to the container, built there and then server runs the built files from within the container, thus leaving the host folder open in VS Code completely disconnected from the server. The solution is to open the application folder inside the docker container in VS Code and then using the source-map there to debug (add breakpoints). 

# Pre-reqs

I'm assuming that you already have a working typescript application that has been set up to run inside a docker container using docker-compose or the docker CLI. 

# VS Code Remote Development

VS Code Remote Development is an extension pack that allows you to open remote folders in your installation of VS Code. For more details on how it works, I highly recommend going through the official documentation available [here](https://code.visualstudio.com/docs/remote/containers).

To get started, install the [extension](https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.vscode-remote-extensionpack) in VS Code.

# Setting Up Project

Now that we've installed the extension, let's set up our application to make sure that debugging works as expected.

## tsconfig.json

We need to make sure that our <code>tsconfig.json</code> is set up to create source-maps for our application so that VS Code can then use those source maps to place breakpoints.

Let's make sure that we have the following lines in <code>tsconfig.json</code>:
```JSON
{
  "compilerOptions": {
    // ... your compiler options go here
    "sourceMap": true,                           /* Generates corresponding '.map' file. */
    "outDir": "dist" /* Redirect output structure to the directory. */,
  },
  "include": ["src"]
}

```

## docker-compose.yml

We need to make sure that we expose the debugger port on our docker application. To do this, add port 9229 under the ports section of your application. 

The file should look something like this:
```yaml
services:
  service-name:
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - ./src:/path/to/app/src
    container_name: container-name
    expose:
      - 4000
    ports:
      - "4000:4000"
      - "9229:9229" # Debugger port
```

## launch.json

Now we need to create a launch configuration for VS Code to attach the debugger to our node application. 

I use this simple configuration:
```json
{
    "version": "0.2.0",
    "configurations": [
      {
        "name": "Docker: Attach to Node",
        "type": "node",
        "request": "attach",
        "restart": true,
        "trace": true,
        "port": 9229,
        "address": "0.0.0.0",
        "localRoot": "${workspaceFolder}/dist",
        "remoteRoot": "/path/to/app/dist", // Note that this is the same location from docker-compose
        "outFiles": ["${workspaceFolder}/dist/**/**.js"],
        "skipFiles": ["<node_internals>/**/*.js"],
        "protocol": "inspector",
        "sourceMaps": true
      }
    ]
  }
```

# Connecting to Container (Remote)

Now we need to run our container in docker and connect VS Code to this container. First launch your container using docker-compose or docker CLI. Then click on the <code>Remote Explorer</code> option in VS Code sidebar. You should see a list of all the running docker containers here. Once you see your container, click on it and select the option <code>Attach to container</code>. This should open up a new instance of VS Code and start setting everything up for remote debugging. You can look at the Debug Console to track the progress. Once that is complete, you need to open the application folder in your container in this instance of VS Code. So click open folder and then select <code>/path/to/app</code> (mount location in docker-compose). You should now see your application files in VS Code along with the <code>dist</code> folder containing the built files and the source-maps.

At this point you're all set to start debugging in VS Code.

# Debugging

Set breakpoints where you want and then go to the <code>Run and Debug</code> tab in VS Code sidebar and run <code>Docker: Attach to Node</code> (the name you set in launch.json).

Now when the line is reached, your code execution should stop there as expected and you should be able use all the debugging features of VS Code with your typescript docker app.

Next time when you open the project, just run the docker container, go to the <code>Remote Explorer</code> tab in VS Code and open your existing remote instance for this container and start debugging.