Angular 9 - Docker 上的 ivy 编译出错

标签 angular docker npm

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

下面是我的 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 目录。

现在,我通过绑定(bind)安装到容器位置/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 目前只有构建命令(假设稍后要做更多事情)

#!/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 版本详细信息:

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 编译来解决问题,但无法解决。感谢有人可以帮助我解决这个问题吗?

最佳答案

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

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

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

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

关于Angular 9 - Docker 上的 ivy 编译出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62323492/

相关文章:

angular - 如何在 ngOnChanges 中处理异步

Angular 2将变量传递给html中的构造函数

javascript - 在 MS Edge 中禁用表单完成

没有特权访问的 Docker VPN IPSec 客户端

Ubuntu 上的 DOCKER_TLS_VERIFY、DOCKER_HOST 和 DOCKER_CERT_PATH

c++ - Windows XP 上的 Meteorite 安装错误 - 找不到 "c:\Microsoft.Cpp.Default.props"

angular - 在 Angular typescript 中从控制台访问类方法

image - 使用docker build输出信息

node.js - 安装node.js后npm -v报错

npm - 如何在 VS2015 中禁用 "npm install"