0

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.

1 Answer 1

2

you may benefit from my current setup:

package.json

"debug": "nodemon --watch 'src/**/*.ts' --ignore 'src/**/*.spec.ts' --exec 'node --inspect=0.0.0.0:9229 --require ts-node/register src/index.ts'"

launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "attach",
      "name": "Docker: Attach to Node",
      "address": "localhost",
      "port": 9229,
      "localRoot": "${workspaceFolder}",
      "remoteRoot": "/usr/src/app",
      "restart": true,
      "skipFiles": ["<node_internals>/**"]
    }
  ]
}

Dockerfile

# syntax=docker/dockerfile:1

ARG NODE_VERSION=20.13.1

# Use node image for base image for all stages.
FROM node:${NODE_VERSION}-alpine as base

# Set working directory for all build stages.
WORKDIR /usr/src/app

COPY package*.json ./
RUN npm install

COPY . .

EXPOSE 3000
EXPOSE 9229

CMD ["npm", "run", "debug"]

compose.yaml

services:
  server:
    build:
      context: .
    environment:
      NODE_ENV: development
    ports:
      - 3000:3000
      - 9229:9229
    volumes:
      - .:/usr/src/app
      - /usr/src/app/node_modules
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.