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

Angular 18执行ng serve报错:src\main.ts未在TypeScript编译中

解决Angular 18.0.6中ng serve报错"File 'src\main.ts' is missing from the TypeScript compilation"的方案

以下是针对该问题的排查与解决步骤:

1. 统一路径分隔符

Windows系统下TypeScript编译器可能对路径分隔符敏感,将tsconfig.app.json里的路径统一改为正斜杠:

{
  "files": [
    "src/main.ts"
  ],
  "include": [
    "src/**/*.d.ts"
  ]
}

2. 彻底清理Angular缓存

缓存可能导致配置不生效,执行以下操作:

ng cache clean --all

手动删除node_modules/.angular文件夹后,重新运行ng serve。

3. 检查根tsconfig.json的排除规则

确认根目录tsconfig.json的exclude字段没有意外排除main.ts:

// 禁止出现以下配置
{
  "exclude": [
    "src/main.ts"
  ]
}

4. 验证文件状态

  • 确认src目录下确实存在main.ts,文件名无拼写错误(如Main.ts、main.tsx)
  • 检查文件权限,确保当前用户拥有读取权限

5. 重装项目依赖

依赖损坏可能引发编译异常,执行:

# Windows用户替换为 rd /s /q node_modules && del package-lock.json
rm -rf node_modules package-lock.json
npm install

6. 对齐Angular CLI版本

确保全局与项目本地CLI版本一致:

ng version

若版本不一致,全局安装对应版本:

npm install -g @angular/cli@18.0.6

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 11:03:10