I am trying to create an angular library project but I am getting this error in the console
Uncaught TypeError: Cannot read property 'id' of undefined
at registerNgModuleType (core.js:34470)
at core.js:34488
at Array.forEach (<anonymous>)
at registerNgModuleType (core.js:34484)
at new NgModuleFactory$1 (core.js:34651)
at compileNgModuleFactory__POST_R3__ (core.js:40286)
at PlatformRef.bootstrapModule (core.js:40609)
at Module../src/main.ts (main.ts:11)
at __webpack_require__ (bootstrap:79)
at Object.0 (main.ts:12)
I have created a new angular project with ng new --enable-ivy
then added a library with ng g library my-lib
. My library has a secondary endpoint with a module (below) so the import would be import { MyLibModule } from 'my-lib/secondary';
@NgModule({
imports: [
CommonModule
],
declarations: [
MyLibDirective
],
exports: [
MyLibDirective
]
})
export class MyLibModule {
public static forRoot(): ModuleWithProviders {
return {
ngModule: MyLibModule,
providers: []
};
}
}
I build this using ng build my-lib
which all builds fine, and I am using link to include the library in the demo app, in the dist folder for the library I run yarn link
and in the root demo app folder I run yarn link my-lib
, then include the module in the imports of the AppModule
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { MyLibModule } from 'my-lib/secondary';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
MyLibModule.forRoot()
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
However when I run yarn start
or ng serve
everything builds fine but I get the above error in the console, debugging this in the browser it would appear that the ngModuleDef
property in my library module does not exist, does anyone know why this happens?
NOTE: This is not a problem if I turn Ivy off but with ng9 coming soon this is not really a viable solution and I would like to know what I have done wrong (if anything) with the library
Output from ng version
_ _ ____ _ ___
/ \ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
/ △ \ | '_ \ / _` | | | | |/ _` | '__| | | | | | |
/ ___ \| | | | (_| | |_| | | (_| | | | |___| |___ | |
/_/ \_\_| |_|\__, |\__,_|_|\__,_|_| \____|_____|___|
|___/
Angular CLI: 8.3.21
Node: 12.12.0
OS: darwin x64
Angular: 8.2.14
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router
Package Version
------------------------------------------------------------
@angular-devkit/architect 0.803.21
@angular-devkit/build-angular 0.803.21
@angular-devkit/build-ng-packagr 0.803.21
@angular-devkit/build-optimizer 0.803.21
@angular-devkit/build-webpack 0.803.21
@angular-devkit/core 8.3.21
@angular-devkit/schematics 8.3.21
@angular/cli 8.3.21
@ngtools/webpack 8.3.21
@schematics/angular 8.3.21
@schematics/update 0.803.21
ng-packagr 5.7.1
rxjs 6.5.4
typescript 3.5.3
webpack 4.39.2