chanaka777
chanaka777

Reputation: 389

Angular 9 - Error with ivy compilation on Docker

I'm trying to build fresh angular 9 project within custom image (based on official node image). Basically I want to cache the package build but not the angular source build.

Below is my 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"]

Ones I created new angular 9 project, I copy the above docker file and run.sh file (described later), and created image angular-cache-image. And also I have updated the angular.json and tsconfig.json files for node_module directory path (replace node_modules with ../node_modules). I have removed dist and node_modules directory from local project directoy.

Now I run container with bind mount to container location /mnt/project and run build command within an entry point. (Note that in container, node_modules in not inside the angular project directoty)

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

run.sh just have the build command at the moment (suppose to do more stuff later)

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

However I'm getting below errors

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 {
                        ~~~~~~~~~~~~~

Below is my local machine angular version details:

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>

And local npm version is 6.14.4

I've tried to resolve with disabling ivy compilation but couldn't work it out. Appreciate of someone can help me on to resolve this ?

Upvotes: 2

Views: 1039

Answers (1)

Charles Desbiens
Charles Desbiens

Reputation: 1079

You are running your build command as the wrong user.

What your image is doing is installing the dependencies and npm as root. However, you're executing your build script as a non-root user by passing in a --user flag, and then running the ng build command as that user in your entry point script.

Ivy doesn't work properly if you compile as a user other than the one that ran NPM install. I haven't done a deep enough dive into Ivy's internals to tell you exactly why, but that is what is happening.

To get around this, you could, and should, just move your build script into your Dockerfile and get rid of your entrypoint; this isn't what an entrypoint is for anyways. Your built image should contain your built application, and therefore should be run at build time, not run time.

Upvotes: 2

Related Questions