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:
- Create a
docker-compose.yml
file, which will start the container instead of VSCode. - Edit your
launch.json
so that it attaches to thenode
in container and restarts debugger session on changes. - 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.