Debugging nestjs application with --watch from vscode with docker stops on changed file Debugging nestjs application with --watch from vscode with docker stops on changed file docker docker

Debugging nestjs application with --watch from vscode with docker stops on changed file


So here's what I found out. When you change code it restarts node's debugger process. VSCode kills Docker container when it loses connection to the debugger.

There is a nice feature which restarts debugger sessions on code changes (see this link) but the problem is - it is for "type": "node" launch configurations. Yours is "type": "docker". From it's options for node only autoAttachChildProcesses seems promising but it doesn't solve the problem (I've checked).

So my suggestion is:

  1. Create a docker-compose.yml file, which will start the container instead of VSCode.
  2. Edit your launch.json so that it attaches to the node in container and restarts debugger session on changes.
  3. Remove/rework tasks.json as it is not needed in it's current state.

docker-compose.yml:

version: "3.0"services:    node:        image: node:14-buster        working_dir: /usr/src/app        command: yarn run start:dev --debug 0.0.0.0:9229        ports:            - 4000:4000            - 9229:9229        volumes:            - ${PWD}:/usr/src/app        environment:            DEBUG: "*"            NODE_ENV: "development"

launch.json:

{    "version": "0.2.0",    "configurations": [        {            "name": "Attach to node",            "type": "node",            "request": "attach",            "restart": true,            "port": 9229        }    ]}

Save the docker-compose.yml in your project root and use docker-compose up to start the container (you may need to install it first https://docs.docker.com/compose/ ). Once it's working start the debugger as usual.