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

Angular容器构建报错:无法找到要导入的样式表

Angular 18 + Material 18 在 node:20-alpine Docker 容器构建失败:找不到@material/feature-targeting样式表

问题重现

本地代码运行正常,但在node:20-alpine Docker容器中执行ng build <my_app>时构建失败,错误信息如下:

35.36 - Building...
35.36 
84.96 Application bundle generation failed. [49.600 seconds]
84.96 
84.98 ✘ [ERROR] Can't find stylesheet to import.
84.98    ╷
84.98 26 │ @use '@material/feature-targeting/feature-targeting';
84.98    │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
84.98    ╵
84.98   node_modules/@material/line-ripple/_line-ripple-theme.scss 26:1                          @use
84.98   node_modules/@material/line-ripple/_line-ripple.scss 29:1                                @forward
84.98   node_modules/@material/line-ripple/_mixins.scss 24:1                                     @use
84.98   node_modules/@material/textfield/_text-field.scss 36:1                                   @forward
84.98   node_modules/@material/textfield/_mixins.scss 24:1                                       @forward
84.98   node_modules/@material/textfield/_index.scss 2:1                                         @use
84.98   node_modules/@angular/material/core/tokens/m2/mat/_form-field.scss 3:1                   @use
84.98   node_modules/@angular/material/core/tokens/m2/_index.scss 20:1                           @use
84.98   node_modules/@angular/material/core/tokens/_m3-tokens.scss 6:1                           @use
84.98   node_modules/@angular/material/core/theming/_definition.scss 6:1                         @forward
84.98   node_modules/@angular/material/_index.scss 7:1                                           @use
84.98   projects/<some_path_in_my_project>1:1  root stylesheet [plugin angular-sass]

涉及版本:Angular 18.0.1、Angular Material 18.0.1、Node.js 20

临时解决方案/Workaround

方案1:切换到非Alpine Node镜像

Alpine镜像使用musl libc,部分npm包在依赖安装或路径解析时可能出现兼容性问题。临时替换为node:20-bullseye-slim或node:20(Debian基础镜像),修改Dockerfile中的基础镜像:

FROM node:20-bullseye-slim

方案2:调整Dockerfile中的依赖安装步骤

在Alpine镜像中,添加基础编译工具并清理npm缓存后重新安装依赖,避免依赖缺失或缓存问题:

FROM node:20-alpine

# 安装Alpine基础编译依赖(解决sass相关编译问题)
RUN apk add --no-cache python3 make g++

# 清理npm缓存并重新安装依赖
RUN npm cache clean --force
WORKDIR /app
COPY package*.json ./
RUN npm install --no-audit --no-fund

# 后续构建步骤
COPY . .
RUN ng build <my_app>

方案3:指定Sass的includePaths

在angular.json的构建配置中,手动添加@material目录到Sass的包含路径,帮助解析器找到Material的SCSS文件:

{
  "projects": {
    "<my_app>": {
      "architect": {
        "build": {
          "options": {
            "sassOptions": {
              "includePaths": ["./node_modules/@material"]
            }
          }
        }
      }
    }
  }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 16:04:58