search envelope-o feed check
Home Unanswered Active Tags New Question
user comment-o

ng build fails if AOT is enabled (Angular 11)

Asked by Nathanael
1 month ago.

I think this might be a bug report, but I'm not sure, so I'm posting it as a questions in case I'm overlooking anything.

Steps to reproduce:
1. Create a new Angular 11 project with `ng new daypilot-test` and accept all defaults
2. Add DayPilot Pro 2021.2.4931 to `package.json` and run `npm install`
3. Add DayPilotModule to the imports in `app.module.ts`
3. Run `ng build --prod`

Build fails with message:
```
Error: node_modules/daypilot-pro-angular/daypilot-angular.d.ts:121: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 (daypilot-pro-angular) which declares DayPilotModule 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.
```

4. Edit `angular.json` and set `aot` and `buildOptimizer` to `false` in the `production` configuration.
5. Run `ng build --prod`
6. It builds successfully, but is not optimized.

This issue looks similar to an old post: https://forums.daypilot.org/question/4964/ivy-schedulermodule-angular-8
In that post it was mentioned that since v2020.1.4238, Ivy + AOT should be supported.

Is there anything I am overlooking? Also, this seems to work just fine with Angular 9 and 10, but not 11.

Comment posted by Dan Letecky [DayPilot]
1 month ago.

What version of @angular/cli do you use?

A project generated using the UI Builder (https://builder.daypilot.org/) that uses version 11.0.0 works fine.

A new project generated using the latest Angular CLI (11.2.8) seems to work fine as well.

I'd try reinstalling @angular/cli, there might be a temporary bad dependency somewhere. That is not unusual - just a couple of minutes ago, generating a new project using "ng new daypilot-test" didn't even complete with a fresh install of @angular/cli@11.2.8 - but it works now.

Comment posted by Nathanael
1 month ago.

Thank for you the pointer in the right direction, Dan.

It looks like I must have some sort of dependency mess going on. Running `npm cache clean --force && rm -rf ./node_modules && npm install` resolved the issue for the fresh test Angular 11.2.8 project I had, but not for my live project.

I'm working on trying to figure that out and will post an update when I have a resolution.

Answer posted by Nathanael
1 month ago.

It looks like this might be buggy behavior on the Angular side (or my lack of understanding of ngcc). The ngcc compiled code is significantly different when automatically run by `ng build` vs when run as an npm postinstall script.

I finally got the project to build again by removing `ngcc` from the `postinstall` script. (Setting this up is mentioned at https://angular.io/guide/ivy#ivy-and-universalapp-shell https://www.reddit.com/r/Angular2/comments/farvxv/angular_9_ngcc_in_postinstall_script/ https://stackoverflow.com/questions/62636918/what-are-the-reasons-for-adding-angular-postinstall-ngcc-script-in-the-proj) The funny thing is that this was working for months...

In case anyone else is wondering. the following rabbit trails did not fix the problem:

  • Downgrading to Node 12 (from 14)
  • Running npm install with --legacy-peer-deps
  • fiddling with tsconfig.json and angular.json
  • Updating the @angular packages
  • Downgrading the @angular packages

Unfortunately. I haven't found the real root cause: why does ngcc generate different code for the same package with exactly the version of Angular (ng --version). If I figure this out. I'll post another update.

Comment posted by Dan Letecky [DayPilot]
1 month ago.

Thanks for posting the solution and the things that didn't work, Nathanael!

New Reply
This reply is
Your name (optional):