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

Angular编译报错TS1149:文件名大小写不一致问题求助

解决Angular编译TS1149错误:文件名大小写不一致导致重复引入

编译Angular项目时遇到TS1149错误,提示文件路径中的MyComponents与MYComponents仅大小写不同,导致文件被重复引入。具体报错:
src/app/app.module.ts:7:35 - 错误 TS1149: 文件名 'E:/Angular Project/hamza-todo-list/src/app/MyComponents/todo-item/todo-item.component.ts' 与已引入的文件名 'E:/Angular Project/hamza-todo-list/src/app/MYComponents/todo-item/todo-item.component.ts' 仅大小写不同。该文件被纳入项目的原因:从文件 'E:/Angular Project/hamza-todo-list/src/app/MYComponents/todos/todos.component.ngtypecheck.ts' 通过 '../todo-item/todo-item.component' 引入;从文件 'E:/Angular Project/hamza-todo-list/src/app/MYComponents/todo-item/todo-item.component.ngtypecheck.ts' 通过 './todo-item.component' 引入;从文件 'E:/Angular Project/hamza-todo-list/src/app/app.module.ts' 通过 './MyComponents/todo-item/todo-item.component' 引入

解决方案

1. 统一磁盘上的文件夹大小写

Windows文件系统默认不区分大小写,但TypeScript编译器严格区分,先把实际文件夹名称统一:

  • 确定一个统一的命名规则(比如MyComponents或全小写mycomponents,保持一致即可)
  • 找到项目中的MyComponents和MYComponents文件夹,修改为统一名称。Windows下可能需要先改成临时名称(比如MyComponents_temp),再改回目标名称,确保系统识别大小写变化

2. 修正所有导入路径的大小写

遍历所有引用该组件的文件,把导入路径的大小写和实际文件夹统一:

  • 打开app.module.ts,修改第7行的导入路径,比如如果实际文件夹是MYComponents,就把./MyComponents/todo-item/todo-item.component改成./MYComponents/todo-item/todo-item.component
  • 检查todos.component.ts等其他文件中的内部导入,确保所有路径的大小写和实际文件完全匹配

3. 清理缓存并重新编译

Angular自动生成的.ngtypecheck.ts文件可能缓存了旧路径,需要清理:

  • 手动删除所有.ngtypecheck.ts文件,或者运行ng clean命令清理项目缓存
  • 重新执行ng serve或ng build,让Angular重新生成正确的类型检查文件

4. 启用严格大小写检查(防复发)

在tsconfig.json中添加配置,强制编译器检查路径大小写一致性,提前避免问题:

{
  "compilerOptions": {
    "forceConsistentCasingInFileNames": true
  }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 08:32:56