【发布时间】: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