Angular 8 通用构建在 SCSS 导入上失败

Angular 8 Universal Build Failed on SCSS Imports

我正在使用 AngularCLI 和 Angular v8。 关于 Angular Universal,我遇到了一个问题,在我 运行 这些命令之后,这些命令是设置的准备步骤:

ng generate universal --clientProject <project_name>

npm install @nguniversal/module-map-ngfactory-loader    
npm install @nguniversal/express-engine

Add ModuleMapLoaderModule on AppServerModule

ng add @nguniversal/express-engine --clientProject <project_name>
npm run build:ssr && npm run serve:ssr

当我运行ng build --configuration staging甚至ng build --prod时,构建成功,没有错误。

但是当我运行ng run app:server:stagingng run app:server:production时,我会遇到如下错误:

ERROR in Module build failed (from ./node_modules/sass-loader/lib/loader.js):

@import 'base/colors';

Can't find stylesheet to import.

....

我的 angular.json 具有以下 SCSS 配置:

"schematics": {
    "@schematics/angular:component": {
      "styleext": "scss"
    }
  },

 ...

 "stylePreprocessorOptions": {
     "includePaths": [
     "src/",
     "src/assets/styles", 
     "node_modules"
    ]
 },

想问一下我是否遗漏了什么或者是否需要更改什么?


已尝试重新安装或运行使用这些命令,但仍然没有成功:

rm -rf node_modules
rm package-lock.json
npm install
npm install node-sass

安装node-sass时,从"Can't find stylesheet to import."错误信息,现在是"File to import not found or unreadable: base/colors."

我遇到了同样的问题“模块构建中的错误失败...找不到要导入的样式表”,而运行使用相同的命令来构建 ssr "Angular Universal"。 由于某种原因,构建过程忽略了 "stylePreprocessorOptions" 路径引用。

解决方案 1:

更改@import 引用以指向文件

例子

@import "~src/assets/styles/apptheme";
@import "~src/assets/styles/functiontheme";

而不是

@import "apptheme";
@import "functiontheme";

解决方案 2:

打开 angular.json 查找 "server" >> "options" 并添加目录引用

"stylePreprocessorOptions": {
          "includePaths": [
            "src/assets/styles"
          ]
        }

示例:

"server": {
      "builder": "@angular-devkit/build-angular:server",
      "options": {
        "outputPath": "dist/server",
        "main": "src/main.server.ts",
        "tsConfig": "tsconfig.server.json",
        "stylePreprocessorOptions": {
          "includePaths": [
            "src/assets/styles"
          ]
        }
      },....

如果 @elias-sh 提供的解决方案都不起作用 -> 只需删除您的 scss 文件并重新创建它。