Javascript – Angular 10 Libraries and IVY/ngcc Compatibility

angularangular-ivyjavascripttypescript

I have a library that needs to be distributed via npm. The recommendation for Angular 10 still seems to be that such libraries should be compiled with IVY disabled but that the Angular CLI will ensure that the library is still compatible with an app that has IVY enabled.

With my library, if I build it with IVY it works as expected. Yet if I disable IVY when building, when I come to import the library I get the following error:

ERROR in node_modules/@me/my-module/lib/my-module.module.d.ts:1: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 (@me/my-module) which declares MeMYModule 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.

Am I missing something? I have found lots of similar problems, but none of the solutions seem to work.

Best Answer

I'd faced similar problem few days ago. I solved it by adding the following script inside my package.json file.

"scripts": {
    "postinstall": "ngcc"
  }