VSCode调试Docker内TypeScript应用出现MODULE_NOT_FOUND错误求助
问题
在VSCode中调试Docker容器内的TypeScript应用,调试器连接后出现以下错误:
[+] Running 1/0 ⠿ Container bot-dev Created 0.1s Attaching to bot-dev bot-dev | bot-dev | > bot@1.0.0 bot-dev bot-dev | > nodemon --signal SIGINT bot/index.ts bot-dev | bot-dev | [nodemon] 2.0.21 bot-dev | [nodemon] to restart at any time, enter `rs` bot-dev | [nodemon] watching path(s): bot/index.ts bot-dev | [nodemon] watching extensions: ts bot-dev | [nodemon] starting `npx --node-options='--require ts-node/register -r tsconfig-paths/register --inspect=0.0.0.0:9229' --nolazy hardhat run --network docker bot/index.ts` bot-dev | Debugger listening on ws://0.0.0.0:9229/ceeb98dc-c9b0-410d-9934-cfeb56eba339 bot-dev | For help, see: https://nodejs.org/en/docs/inspector bot-dev | Debugger attached. bot-dev | node:internal/modules/cjs/loader:988 bot-dev | throw err; bot-dev | ^ bot-dev | bot-dev | Error: Cannot find module '/Applications/VisualStudioCode.app/Contents/Resources/app/extensions/ms-vscode.js-debug/src/bootloader.bundle.js' bot-dev | Require stack: bot-dev | - internal/preload bot-dev | at Module._resolveFilename (node:internal/modules/cjs/loader:985:15) bot-dev | at Module._load (node:internal/modules/cjs/loader:833:27) bot-dev | at Module.require (node:internal/modules/cjs/loader:1051:19) bot-dev | at Module._preloadModules (node:internal/modules/cjs/loader:1326:12) bot-dev | at loadPreloadModules (node:internal/process/pre_execution:597:5) bot-dev | at prepareMainThreadExecution (node:internal/process/pre_execution:104:3) bot-dev | at node:internal/main/run_main_module:10:1 { bot-dev | code: 'MODULE_NOT_FOUND', bot-dev | requireStack: [ 'internal/preload' ] bot-dev | } bot-dev | bot-dev | Node.js v18.10.0 bot-dev | Waiting for the debugger to disconnect...
使用的docker-compose配置:
version: '3.8' services: bot-dev: image: node:18.10.0-alpine3.16 container_name: bot-dev volumes: - ./../../:/app working_dir: /app ports: - 3000:300 - 9229:9229 - 8082:8545 command: - /bin/sh - -c - | npm run bot-dev
VSCode的launch.json配置:
{ // Use IntelliSense to learn about possible attributes. // Hover to view descriptions of existing attributes. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "Bot", "type": "node", "request": "attach", "address": "localhost", "port": 9229, "restart": true, "localRoot": "${workspaceFolder}", "sourceMaps": true }, ] }
报错模块本地存在,但调试器似乎在容器内查找该模块。未启动调试时应用运行正常,已尝试重装容器内node_modules、禁用autoAttach,均无效。
解决方案
问题核心是VSCode调试器尝试将本地的bootloader模块注入容器内Node进程,但容器中无对应路径文件,通过以下配置调整解决:
更新launch.json,添加路径过滤配置
修改后的launch.json如下:{ "version": "0.2.0", "configurations": [ { "name": "Bot", "type": "node", "request": "attach", "address": "localhost", "port": 9229, "restart": true, "localRoot": "${workspaceFolder}", "sourceMaps": true, "skipFiles": [ "<node_internals>/**", "/Applications/VisualStudioCode.app/**" ], "resolveSourceMapLocations": [ "${workspaceFolder}/**", "!**/node_modules/**" ] } ] }skipFiles指定调试器跳过VSCode自身bootloader文件和Node内部文件,避免加载不存在的模块resolveSourceMapLocations限定仅解析项目内的source map,规避跨环境路径冲突
调整nodemon启动命令,避免调试参数冲突
修改package.json中的bot-dev脚本,移除重复的--inspect相关参数:"scripts": { "bot-dev": "nodemon --signal SIGINT --exec 'npx ts-node -r tsconfig-paths/register' bot/index.ts" }调试器会负责附加进程,无需在脚本中指定
--inspect参数确保容器内依赖完整
执行容器内的依赖安装命令,确保ts-node、tsconfig-paths等依赖正确安装:docker-compose exec bot-dev npm install重启容器与调试会话
先停止并删除容器:docker-compose down重新启动容器:
docker-compose up -d最后重新启动VSCode调试会话
内容的提问来源于stack exchange,提问作者Arnau Amargant Domene
相关产品推荐
相关产品推荐

