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

新建Angular项目执行ng serve编译失败问题求助

解决Angular项目ng serve时的模块未找到及CSS加载错误

用ng new创建新Angular项目后,执行ng serve编译时出现以下错误:

Error: Module not found: Error: Can't resolve 'F:/C#/CCC/src/app/app.component.css?ngResource' in '[Path]'
Error: Module not found: Error: Can't resolve 'F:\C#\CCC\node_modules\webpack-dev-server\client\index.js?protocol=auto%3A&username=&password=&hostname=0.0.0.0&port=0&pathname=%2Fng-cli-ws&logging=info&reconnect=10' in '[Path]'
Error: The loader "F:/C#/CCC/src/app/app.component.css" didn't return a string.

解决方案:

  • 移除路径中的特殊字符
    项目路径包含#这类特殊字符会导致webpack解析路径出错,直接把整个项目移到不含特殊字符的路径(比如F:/AngularProjects/CCC),之后重新执行npm install,再运行ng serve。

  • 清理并重装依赖
    如果换路径不方便,先清理损坏的依赖:
    Windows命令:

    rmdir /s /q node_modules
    del package-lock.json yarn.lock
    npm install
    

    类Unix系统命令:

    rm -rf node_modules package-lock.json yarn.lock
    npm install
    
  • 检查并修复CSS文件
    打开src/app/app.component.css确认文件存在且内容正常,若文件为空或损坏,删除后重新创建该文件,确保文件编码为UTF-8。

  • 更新Angular CLI到最新版本
    全局更新CLI:

    npm uninstall -g @angular/cli
    npm install -g @angular/cli@latest
    

    项目内同步更新:

    ng update @angular/cli @angular/core
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.23 14:54:22