Answer a question

I have been trying to figure out for a while how I would go about setting up a attachment to node debugging processes that are exposed in my environment from multiple running nestjs apps within a mono-repo setup. (With VS code)

https://github.com/bozvul993/nest-testing-mono-repo-debug

Ideally i want the debugging sessions restarted on code changes [If this is possible], but more importantly working.

I have provided a repository with my sample project. To run the apps inside /docker folder docker-compose -f dev.yml up This brings up the three apps in the monorepo. All apps exposed to the host machine their default node debugging ports...

My vs code launch configuration that i used to attempt this i included:

            "type": "node",
            "request": "attach",
            "name": "Debug App1",
            "address": "0.0.0.0",
            "port": 9231,
            "localRoot": "${workspaceFolder}/mono-repo",
            "remoteRoot": "/app/mono-repo",
            "trace": true,
            "restart": true,
            "sourceMaps": true,
            "skipFiles": [
                "<node_internals>/**"
            ]
        }


With Web-storm this was easier to achieve somehow..

Answers

I found this https://code.visualstudio.com/docs/containers/debug-node to be quite useful.

Long story short, my docker-compose file looks like

version: '3.7'
services:
  api:
    container_name: api
    build:
      context: .
      target: development
    volumes:
      - '.:/app'
      - './node_modules:/app/node_modules'
    command: yarn start:debug
    ports:
      - ${API_PORT}:${API_PORT}
      - 9229:9229
    networks:
      - network
  mongo_db:
    ...
    ...
    ...
networks: 
  network:
    driver: bridge

Where the development part of the Docker file picked from the docker-compose.yaml file looks like

FROM node:16-alpine as development
ARG NODE_ENV=development
ENV NODE_ENV=${NODE_ENV}
WORKDIR /app

COPY package.json .
COPY yarn.lock .

RUN yarn

COPY . .

RUN yarn build

FROM node:16-alpine as production
...

And my launch.json looks like

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Debug: api",
      "type": "node",
      "request": "attach",
      "restart": true,
      "port": 9229,
      "address": "0.0.0.0",
      "localRoot": "${workspaceFolder}",
      "remoteRoot": "/app",
      "protocol": "inspector",
      "skipFiles": ["<node_internals>/**"]
    }
  ]
}

And finally, but not least important, the start:debug parts of package.json must be altered a little.

Mine looks like

"start:debug": "nest start --debug 0.0.0.0:9229 --watch",

Works on my machine :) by first starting all the containers using docker compose up -d, and then starting the debugging process from VS code.

VS code 1.57.1, Docker version 20.10.7, MacOS

Logo

开发云社区提供前沿行业资讯和优质的学习知识,同时提供优质稳定、价格优惠的云主机、数据库、网络、云储存等云服务产品

更多推荐