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

使用NX创建Angular应用启动报错:无法解构未定义的'pos'属性

问题

按以下步骤创建Monorepo及Angular应用后,执行启动命令时出现错误:

  1. 执行初始化命令:npx create-nx-workspace@latest
  2. 生成Angular应用:nx g @nx/angular:app apps/frontend

运行nx serve frontend启动应用时,抛出错误:

✘ [ERROR] Cannot destructure property 'pos' of 'file.referencedFiles[index]' as it is undefined. [plugin angular-compiler]

node_modules/typescript/lib/typescript.js:125339:9:
125339 │ ({ pos, end } = file.referencedFiles[index]);

请问该错误的原因是什么,如何修复?

解决方案

这个错误的核心原因是TypeScript版本与NX、Angular的编译插件不兼容——默认创建Workspace时会拉取最新版TypeScript,而当前NX Angular插件的编译逻辑还未适配该版本的TS API,导致解构file.referencedFiles[index]时出现未定义的情况。

修复步骤:

  1. 降级TypeScript到兼容版本(推荐4.9.x系列,与多数NX 16.x版本适配):
    npm install typescript@~4.9.0 --save-dev
    
  2. 清理旧依赖并重新安装:
    rm -rf node_modules package-lock.json
    npm install
    
  3. 重新启动应用:
    nx serve frontend
    

也可以在创建Workspace时直接指定兼容的NX版本,避免自动安装不兼容的TS版本:

npx create-nx-workspace@latest --nx-version 16.0.0 --package-manager npm

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 05:05:56