You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用VSCode调试本地Docker容器中的TypeScript Node.js应用

Node.js + Docker + Nodemon 调试断点异常问题解决思路

问题现象

调试器成功连接(显示Debugger attached.),但断点无法停在预设位置,反而跳转到同名但内容不符的.ts文件。

现有配置

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

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\""
  },
...

launch.json

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

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

tsconfig.json

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

解决思路

  • 排查文件同步冲突:Dockerfile中先COPY了项目文件,后续又通过volume挂载本地目录到/var/www,可能导致容器内存在新旧文件混合。建议构建镜像时仅保留依赖安装步骤,代码完全通过volume挂载,或者清理容器内残留的旧代码文件。

  • 优化nodemon调试命令:
    给ts-node添加--files参数确保类型解析完整,同时简化命令:

    "debug": "nodemon --exec \"node --inspect-brk=0.0.0.0:5858 --require ts-node/register/transpile-only --files ./src/index.ts\""
    

    或者改用ts-node直接启动调试:

    "debug": "nodemon --exec \"ts-node --inspect-brk=0.0.0.0:5858 ./src/index.ts\""
    
  • 调整VSCode调试配置:
    添加trace: true查看源映射解析日志,定位映射错误环节;同时确认localRoot和remoteRoot路径完全匹配:

    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "API Debug",
                "type": "node",
                "request": "attach",
                "port": 5858,
                "restart": true,
                "sourceMaps": true,
                "trace": true,
                "outFiles": [],
                "resolveSourceMapLocations": [
                    "${workspaceFolder}/**",
                    "!**/node_modules/**"
                ],
                "localRoot": "${workspaceFolder}",
                "remoteRoot": "/var/www"
            }
        ]
    }
    
  • 清理容器缓存:
    重新构建镜像并清理旧容器,避免缓存导致的代码不一致:

    docker-compose build --no-cache api
    docker-compose down && docker-compose up
    
  • 优化tsconfig配置:
    明确sourceRoot为容器内项目根路径,同时开启inlineSources提升映射准确性:

    {
      "compilerOptions": {
        "sourceMap": true,
        "inlineSources": true,
        "outDir": "dist",
        "strict": true,
        "lib": ["esnext"],
        "sourceRoot": "/var/www",
        "esModuleInterop": true
      }
    }
    
  • 验证nodemon重启逻辑:
    检查nodemon配置(若存在nodemon.json),确保没有忽略需要调试的文件;确认launch.json的restart: true能在nodemon重启进程后自动重新附加调试器。


内容的提问来源于stack exchange,提问作者mrodo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 08:11:05