Angular 15.1.3项目启动报错:无法找到Bootstrap及Material主题SCSS文件
解决Angular 15.1.3中Sass样式导入失败的问题
1. 移除Sass导入语句中的~前缀
Angular 14+ 配套的sass-loader已废弃~前缀指向node_modules的写法,直接删除所有@import语句里的~:
- 将
@import '~bootstrap/scss/bootstrap';改为@import 'bootstrap/scss/bootstrap'; - 将
@import '~@angular/material/theming';改为@import '@angular/material/theming';
2. 修正stylePreprocessorOptions配置
当前includePaths仅配置了node_modules/@angular,需补充根目录node_modules,让Sass能全局检索所有第三方包:
修改angular.json中build.options.stylePreprocessorOptions:
"stylePreprocessorOptions": { "includePaths": [ "node_modules", "node_modules/@angular" ] },
3. 清理重复的Bootstrap引入
你的styles数组同时引入了Bootstrap的SCSS源文件和编译后的CSS文件,会导致样式重复冲突,保留SCSS版本即可(方便后续自定义样式):
删除"node_modules/bootstrap/dist/css/bootstrap.min.css",这一行。
4. 重置依赖并重新构建
执行以下命令确保依赖完整无损坏:
rm -rf node_modules package-lock.json npm install ng serve
额外排查(若问题仍存在)
- 执行
npm list @angular/material bootstrap,确认两个依赖已正确安装 - 检查
node_modules/@angular/material目录下是否存在_theming.scss文件(Sass局部文件以下划线开头,导入时可省略下划线)
内容的提问来源于stack exchange,提问作者DanConsultant
相关产品推荐
相关产品推荐

