【问题标题】:Angular 9 - Error with ivy compilation on DockerAngular 9 - Docker 上的 ivy 编译出错
【发布时间】:2020-10-01 01:03:37
【问题描述】:

我正在尝试在自定义图像中构建新的 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

我尝试通过禁用 ivy 编译来解决问题,但无法解决。感谢有人可以帮助我解决这个问题?

【问题讨论】:

    标签: angular docker npm


    【解决方案1】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-30
      • 1970-01-01
      • 2020-06-17
      • 2019-10-31
      • 2020-06-07
      • 1970-01-01
      • 2020-02-19
      • 1970-01-01
      相关资源
      最近更新 更多