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

Angular 10.x在Node 14.15.0构建时遇循环依赖警告及栈溢出错误

Angular 10 + Node 14构建报错:循环依赖警告+栈溢出+--trace-warnings参数无效

问题场景

Angular 10.x项目在Node 10.14.1环境下可正常构建运行,但切换至Node 14.15.0后出现以下循环依赖警告:

Building Angular Package
(node:9555) Warning: Accessing non-existent property 'lineno' of module exports inside circular dependency
(Use `node --trace-warnings ...` to show where the warning was created)
(node:9555) Warning: Accessing non-existent property 'column' of module exports inside circular dependency
(node:9555) Warning: Accessing non-existent property 'filename' of module exports inside circular dependency
(node:9555) Warning: Accessing non-existent property 'lineno' of module exports inside circular dependency
(node:9555) Warning: Accessing non-existent property 'column' of module exports inside circular dependency
(node:9555) Warning: Accessing non-existent property 'filename' of module exports inside circular dependency

最终触发栈溢出错误:

Compiling TypeScript sources through ngc
ERROR: Maximum call stack size exceeded
An unhandled exception occurred: Maximum call stack size exceeded

尝试执行npm run rebuild-projects --verbose --trace-warnings时,--trace-warnings参数未生效,终端仍提示(Use node --trace-warnings ...)。补充:Mac环境下出现问题,同事Windows环境可正常构建。


解决方案

1. 修复--trace-warnings参数无效问题

npm run的参数传递逻辑是:Node相关参数需要直接传给Node进程,而非npm脚本。有两种实现方式:

  • 临时命令调用:跳过npm脚本,直接用Node启动Angular CLI并传入参数:
    node --trace-warnings node_modules/@angular/cli/bin/ng build [你的原有构建参数]
    
    (如果rebuild-projects脚本是自定义逻辑,替换成对应脚本的Node启动路径)
  • 永久修改package.json:在脚本中直接指定Node参数:
    "scripts": {
      "rebuild-projects": "node --trace-warnings node_modules/@angular/cli/bin/ng build [原有参数]"
    }
    

执行后就能看到警告的完整调用栈,定位循环依赖的具体模块。

2. 解决循环依赖与栈溢出问题

这些问题本质是Node 14对循环依赖的处理更严格,加上Angular 10与Node 14的兼容性细节导致:

  • 扫描并解除TypeScript循环依赖:
    使用madge工具扫描项目依赖:
    npx madge --circular src/
    
    根据扫描结果重构代码,比如将循环引用的公共逻辑抽离为独立模块。
  • 升级Angular依赖版本:
    Angular 10官方支持Node 10/12,对Node 14的兼容性有限。可以升级到Angular 10.x的最新补丁版本,或者直接升级到Angular 11(完全支持Node 14):
    ng update @angular/cli@10 @angular/core@10
    # 或升级到Angular 11
    ng update @angular/cli@11 @angular/core@11
    
  • 清理依赖缓存(Mac专属):
    Mac下可能存在依赖缓存不一致问题,执行以下命令重置依赖:
    rm -rf node_modules package-lock.json
    npm cache clean --force
    npm install
    
  • 检查文件路径大小写:
    Mac是大小写敏感系统,Windows则不是。检查所有import语句的文件路径是否与实际文件大小写完全一致,路径不匹配可能触发异常加载逻辑,间接导致循环依赖。

3. 临时规避方案(不推荐长期使用)

若暂时无法重构或升级,可通过环境变量禁用循环依赖警告,但无法解决栈溢出问题:

export NODE_OPTIONS="--no-warnings"
npm run rebuild-projects

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 08:32:48