首页 > 解决方案 > Angular 9 - Docker 上的 ivy 编译出错

问题描述

我正在尝试在自定义图像中构建新的 angular 9 项目(基于官方节点图像)。基本上我想缓存包构建而不是角度源构建。

下面是我的 Dockerfile

FROM    node

COPY    package.json /mnt/
RUN     cd /mnt/ && npm install && npm audit fix && npm install -g @angular/cli

ENV     PATH /mnt/node_modules/.bin:$PATH
RUN     mkdir /mnt/project
WORKDIR /mnt/project

ENTRYPOINT     ["/bin/bash", "run.sh"]

我创建了新的 angular 9 项目,我复制了上面的 docker 文件和 run.sh 文件(稍后描述),并创建了图像 angular-cache-image。而且我还更新了 node_module 目录路径的 angular.json 和 tsconfig.json 文件(替换node_modules../node_modules)。我已经从本地项目目录中删除了 dist 和 node_modules 目录。

现在我运行容器并绑定挂载到容器位置 /mnt/project 并在入口点内运行构建命令。(请注意,在容器中,node_modules 不在 Angular 项目目录中)

docker container run -it --rm --name angular-cache-test -v $(pwd):/mnt/project --user $(id -u):$(id -g) angular-cache-image

run.sh 目前只有 build 命令(假设以后做更多的事情)

#!/bin/bash
npm -v
node -v
ng build --prod

但是我遇到了错误

6.13.7
v13.10.1



ERROR in ../node_modules/@angular/platform-browser/platform-browser.d.ts:44:22 - error NG6002: Appears in the NgModule.imports of AppModule, but could not be resolved to an NgModule class.

This likely means that the library (@angular/platform-browser) which declares BrowserModule has not been processed correctly by ngcc, or is not compatible with Angular Ivy. Check if a newer version of the library is available, and update if so. Also consider checking with the library's authors to see if the library is expected to be compatible with Ivy.

44 export declare class BrowserModule {
                        ~~~~~~~~~~~~~

以下是我的本地机器角度版本详细信息:

Angular CLI: 9.0.2
Node: 12.18.0
OS: linux x64

Angular: <error>
... animations, cli, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router
Ivy Workspace: Yes

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.900.2 (cli-only)
@angular-devkit/build-angular   <error>
@angular-devkit/core            9.0.2 (cli-only)
@angular-devkit/schematics      9.0.2 (cli-only)
@schematics/angular             9.0.2 (cli-only)
@schematics/update              0.900.2 (cli-only)
rxjs                            6.5.3 (cli-only)
typescript                      <error>

本地 npm 版本为 6.14.4

我试图通过禁用常春藤编译来解决,但无法解决。有人可以帮助我解决这个问题吗?

标签: angulardockernpm

解决方案


您正在以错误的用户身份运行构建命令。

您的图像正在做的是以 root 身份安装依赖项和 npm。但是,您通过传入--user标志以非 root 用户身份执行构建脚本,然后在入口点脚本中以该用户身份运行 ng build 命令。

如果您作为运行 NPM 安装的用户以外的用户进行编译,Ivy 将无法正常工作。我还没有深入了解 Ivy 的内部结构,无法确切地告诉你原因,但这就是正在发生的事情。

为了解决这个问题,您可以而且应该将您的构建脚本移动到您的 Dockerfile 中并摆脱您的入口点;无论如何,这不是入口点。您构建的映像应该包含您构建的应用程序,因此应该在构建时运行,而不是在运行时运行。


推荐阅读