Docker中Nest实现HMR启动时遇Maximum call stack size exceeded错误求助
Nest项目Docker环境下HMR启动报错(Maximum call stack size exceeded)解决思路
报错信息
/node_modules/enhanced-resolve/lib/Resolver.js:433 if (err) return callback(err); ^ RangeError: Maximum call stack size exceeded at /node_modules/enhanced-resolve/lib/Resolver.js:433:5 at eval (eval at create (/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:27:1)
当前环境版本
npm version { api: '0.0.1', npm: '7.24.2', node: '14.18.1', }
Dockerfile内容
RUN npm install -g npm@7 RUN npm cache clean --force RUN npm install RUN npm run build
package.json依赖配置
"dependencies": { "@nestjs/common": "^9.0.0", "@nestjs/config": "^2.2.0", "@nestjs/core": "^9.0.0", "@nestjs/jwt": "^9.0.0", "@nestjs/mapped-types": "*", "@nestjs/passport": "^9.0.0", "@nestjs/platform-express": "^9.0.0", "@nestjs/serve-static": "^3.0.0", "@nestjs/typeorm": "^9.0.1", "bcrypt": "^5.0.0", "nestjs-typeorm-paginate": "^4.0.2", "passport": "^0.6.0", "passport-jwt": "^4.0.0", "pg": "^8.6.0", "reflect-metadata": "^0.1.13", "rimraf": "^3.0.2", "rxjs": "^7.2.0", "slugify": "^1.4.5", "typeorm": "^0.3.10", "uuid": "^9.0.0" }, "devDependencies": { "@nestjs/cli": "^9.1.5", "@nestjs/schematics": "^9.0.3", "@nestjs/testing": "^9.2.0", "@types/express": "^4.17.13", "@types/jest": "^28.1.8", "@types/node": "^16.0.0", "@types/passport-jwt": "^3.0.7", "@types/supertest": "^2.0.11", "@typescript-eslint/eslint-plugin": "^5.0.0", "@typescript-eslint/parser": "^5.0.0", "eslint": "^8.0.1", "eslint-config-prettier": "^8.3.0", "eslint-plugin-prettier": "^4.0.0", "jest": "^28.0.1", "prettier": "^2.3.2", "run-script-webpack-plugin": "^0.1.0", "source-map-support": "^0.5.20", "supertest": "^6.1.3", "ts-jest": "^28.0.8", "ts-loader": "^9.2.3", "ts-node": "^10.0.0", "tsconfig-paths": "^4.1.0", "typescript": "^4.9.3", "webpack": "^5.75.0", "webpack-node-externals": "^3.0.0" }, "jest": { "moduleFileExtensions": [ "js", "json", "ts" ], "rootDir": "src", "testRegex": ".spec.ts$", "transform": { "^.+\\.(t|j)s$": "ts-jest" }, "coverageDirectory": "../coverage", "testEnvironment": "node" }, "main": "index.js"
已尝试方案
- 删除node_modules后重新安装
- 更换Node和npm版本
解决思路
1. 修复Webpack配置的循环解析问题
报错来自enhanced-resolve(Webpack核心依赖),大概率是配置导致的循环引用或解析冲突:
- 调整
webpack-node-externals配置,将热更新相关模块加入白名单,避免解析时跳过必要的HMR依赖:externals: [nodeExternals({ allowlist: ['webpack/hot/poll?100'] })] - 检查
run-script-webpack-plugin的使用,确保没有重复触发解析逻辑,插件配置仅保留启动热更新服务的必要参数。
2. 拆分Docker开发/生产构建流程
当前Dockerfile是生产构建逻辑,HMR属于开发特性,需单独配置开发镜像:
- 开发阶段跳过
npm run build,直接挂载本地代码目录到容器,用热更新脚本启动:FROM node:14.18.1 AS dev WORKDIR /app COPY package*.json ./ RUN npm install -g npm@7 && npm install COPY . . CMD ["npm", "run", "start:hmr"] - 生产阶段使用多阶段构建,仅复制编译后的代码和生产依赖。
3. 锁定依赖版本,消除版本冲突
- 将
@nestjs/mapped-types的*版本改为具体兼容版本(如^1.2.0),通配符易导致依赖树混乱。 - 执行
npm ls enhanced-resolve检查是否存在多版本依赖,若有,在package.json中强制统一版本:
之后重新执行"resolutions": { "enhanced-resolve": "^5.10.0" }npm install。
4. 验证Nest HMR基础配置
- 确保
main.ts正确启用热更新:async function bootstrap() { const app = await NestFactory.create(AppModule); await app.listen(3000); if (module.hot) { module.hot.accept(); module.hot.dispose(() => app.close()); } } bootstrap(); - 确认
package.json的start:hmr脚本配置正确,关联对应的Webpack HMR配置文件:"scripts": { "start:hmr": "nest build --webpack --webpackPath webpack-hmr.config.js --watch" }
5. 清理Docker缓存,重新构建
Docker缓存可能残留旧依赖,执行以下命令彻底清理后重新构建:
docker system prune -a docker build --no-cache -t nest-hmr-dev .
内容的提问来源于stack exchange,提问作者TheCallofDuty
相关产品推荐
相关产品推荐

