【问题标题】:vscode typescript node.js debugging inside docker breakpoint set but not yet boundvscode typescript node.js在docker断点集内调试但尚未绑定
【发布时间】:2020-08-19 20:53:14
【问题描述】:

我正在尝试在vscode 中调试typescript node.js dockerized app

我有以下结构:

|api
|- src
|- dist

我的DockerFile是这样的:

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" ]

我的主文件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}`);
});

我的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"]
}

命令yarn start 到我的DockerFile 执行`nodemon 配置如下:

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

这是我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

最后我的launch.json如下:

{
  "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
    }
  ]
}

我的服务正常启动,我的vscode 调试器似乎正确附加:

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.

但是当我尝试放置 breakpoint 时,结果如下:

我已经阅读了很多关于这个问题的信息,但没有一个解决方案对我有用。

我也把vscode的日志文件放在这里了:https://mega.nz/file/kfZTyKoK#cZxcVJnTgzJKzHH_Xj6SVbNbVFmYlMWOwH6-OAQMfRw

如果您需要任何额外的信息,请告诉我。

提前致谢。

【问题讨论】:

    标签: node.js typescript docker debugging visual-studio-code


    【解决方案1】:

    事实证明,通过使用NODE_OPTIONS=--inspect 作为环境变量,每个进程都试图启动检查器。但是由于我是用 yarn 启动我的应用程序,所以 yarn 进程得到了检查器,而我的节点进程无法绑定到那个端口。

    所以 vscode 调试了错误的进程,这就是断点设置不正确的原因。

    【讨论】:

    • 那么你是如何解决这个问题的?还有其他方法可以通过参数吗?
    猜你喜欢
    • 1970-01-01
    • 2020-10-22
    • 2021-11-21
    • 2020-05-18
    • 2020-10-04
    • 2021-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多