How to to debug a Typescript NodeJS app running inside a local Docker container with VScode

45 views Asked by At

Here is my Dockerfile:

FROM node:20-alpine

WORKDIR /var/www/

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 80
EXPOSE 5858

# Run the application
CMD [ "npm", "run debug" ]

Here the relevant part of my package.json:

...
  "scripts": {
    "start": "npx prisma generate && npx prisma db push && npx prisma db seed && ts-node -r newrelic ./src/index.ts",
    "dev": "npx prisma generate && nodemon ./src/index.ts",
    "debug": "nodemon --exec \"node --inspect-brk=0.0.0.0:5858 --require ts-node/register ./src/index.ts\""
  },

...

Here is my launch.json (almost entirely lifted from Docker's own documentation):

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "API Debug",
            "type": "node",
            "request": "attach",
            "port": 5858,
            "restart": true,
            "sourceMaps": true,
            "outFiles": [],
            "resolveSourceMapLocations": [
                "${workspaceFolder}/**",
                "!**/node_modules/**"
            ],
            "localRoot": "${workspaceRoot}/",
            "remoteRoot": "/var/www"
        }
    ]
}

Here is my docker-compose.yaml:

version: '2.3'

services:
  db:
    # a MySQL DB server, not relevant

  api:
    build: <path-to-project>
    depends_on:
      - db
    restart: always
    scale: 1
    environment:
      # a bunch of env variables which are not relevant to the problem
    ports:
      - "80:80
      - "5858:5858"
    volumes:
      - <path-to-project>:/var/www

And here is my tsconfig.json

{
  "compilerOptions": {
    "sourceMap": true,
    "outDir": "dist",
    "strict": true,
    "lib": ["esnext"],
    "sourceRoot": "./",
    "esModuleInterop": true
  }
}

I do get the Debugger listening on ws://0.0.0.0:5858/112561df-f21d-4ad5-87cc-7dc5ece76b0e message, and then the Debugger attached. message when I click on the green play button in VSCode.

However, the problem I have is that the debugger won't pass through the breakpoints I set, but use some other .ts files with the same names, but which look different.

Any ideas?

I already took a look at this question but in my case I have the added complexity of nodemon which isn't mentioned there.

0

There are 0 answers