2

I'm trying to debug a typescript node.js dockerized app inside vscode.

I have the following structure:

|api
|- src
|- dist

My DockerFile is like that:

FROM node:latest

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
COPY package.json yarn.lock ./
RUN yarn

# Bundle app source
COPY . .

# Build typescript source
RUN npx tsc

EXPOSE 8080
EXPOSE 9229
CMD [ "yarn", "start" ]

My main file server.ts :

import { Request, Response } from 'express';
import express = require('express');
import os = require('os');

const app = express();
const port = 8080;

app.get('/', (req: Request, res: Response) => {
  res.send(`<h3>It's ${os.hostname()} - (API) - ${port}</h3>`);
});
app.listen(port, () => {
  console.log(`[API] Server Started on Port ${port}`);
});

My tsconfig.json

{
  "compilerOptions": {
    "target": "esnext",
    "module": "commonjs",
    "lib": ["es6"],
    "allowJs": true,
    "outDir": "dist",
    "strict": true,
    "noImplicitAny": true,
    "esModuleInterop": true,
    "resolveJsonModule": true,
    "removeComments": true,
    "preserveConstEnums": true,
    "baseUrl": ".",
    "paths": {
      "@/*": ["src/*"]
    },
    "declaration": true,
    "sourceMap": true
  },
  "include": ["src/**/*.ts"]
}

The command yarn start into my DockerFile execute `nodemon which is configured like that:

{
  "restartable": "rs",
  "watch": ["src"],
  "ext": ".ts,.js",
  "ignore": [".git", "node_modules/**/node_modules"],
  "exec": "npx tsc && node ./dist/server.js",
  "signal": "SIGINT"
}

Here is the service configuration inside my docker-compose.yml

version: "3.8"
services:
  api:
    build: api/
    environment:
      - NODE_ENV=development
      - NODE_OPTIONS=--inspect=0.0.0.0:9229
    deploy:
      replicas: 1
      restart_policy:
        max_attempts: 3
        condition: on-failure
      update_config:
        parallelism: 1
        delay: 1s
    volumes:
      - ./api:/usr/src/app
      - node_modules-api:/usr/src/app/node_modules
    ports:
      - 9229:9229

And finally my launch.json is as following:

{
  "configurations": [
    {
      "type": "node",
      "request": "attach",
      "name": "Attach to API",
      "port": 9229,
      "protocol": "inspector",
      "cwd": "${workspaceFolder}/api/",
      "localRoot": "${workspaceFolder}/api/",
      "remoteRoot": "/usr/src/app/",
      "outFiles": ["${workspaceRoot}/api/dist/**/*.js"],
      "sourceMaps": true,
      "trace": "verbose",
      "stopOnEntry": true
    }
  ]
}

My service is starting correctly and my vscode debugger seems to attach correctly:

Debugger listening on ws://0.0.0.0:9229/41993b9a-4af4-4106-b49f-0fe601e6571e

For help, see: https://nodejs.org/en/docs/inspector

yarn run v1.22.4

$ node src/wrapper.js

Starting inspector on 0.0.0.0:9229 failed: address already in use

[nodemon] 2.0.4


[nodemon] to restart at any time, enter `rs`

[nodemon] watching path(s): src/**/*

[nodemon] watching extensions: ts,js


[nodemon] starting `npx tsc && node ./dist/server.js`


[API] Server Started on Port 8080


Debugger attached.

But when I'm trying to place a breakpoint here is the following result:

enter image description here

I've read a lot about the issue but none of the delivred solution worked for me.

I also dropped the log file of vscode here: https://mega.nz/file/kfZTyKoK#cZxcVJnTgzJKzHH_Xj6SVbNbVFmYlMWOwH6-OAQMfRw

If you need any extra informations, please tell me.

Thanks in advance.

1 Answer 1

1

It turns out that by using NODE_OPTIONS=--inspect as an env var every process tried to start the inspector. But since I was starting my app with yarn, the yarn process got the inspector and my node process couldn't bind to that port.

So vscode was debugging the wrong process which is why breakpoint weren't being set correctly.

Sign up to request clarification or add additional context in comments.

1 Comment

So how did you work around that? Is there other ways to pass through the argument?

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.