Angular library build error: TypeError: Cannot read property 'type' of null
Update
It seems like my diagnosis of this issue was incorrect (it's been so long now I can't remember exactly how I fixed it). Check out this issue in the Angular repo for what sounds like the correct diagnosis,
Original answer:
So after a LOT of debugging, I figured it out:
My custom library, let call it library A, imports another custom library, library B, into library A's NgModule
. Library B exports several components and modules from it's main NgModule
. The problem was that, while library B exported the components and modules from it's NgModule
, I failed to also export those components and modules via javascript/typescript. The solution was to make sure to export any components / modules via typescript that I exported in the NgModule
.
Example problem code
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { LibraryBComponent } from './library-b.component';
@NgModule({
imports: [CommonModule],
declarations: [LibraryBComponent],
exports: [LibraryBComponent],
})
export class LibraryBModule {}
Solution code
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { LibraryBComponent } from './library-b.component';
export { LibraryBComponent } // <-- addition
@NgModule({
imports: [CommonModule],
declarations: [LibraryBComponent],
exports: [LibraryBComponent],
})
export class LibraryBModule {}
Know this thread is kinda old but this may help someone coming across this.
I was/am having the same issue with my Angular 9 library that is wrapping around some Angular Material components both my library and my app that is consuming my library are using Ivy.
Adding the fullTemplateTypeCheck and setting it to false suppresses the error however the down side is it may suppress other errors you may be possibly getting.
https://github.com/ng-packagr/ng-packagr/issues/1087
Example tsconfig.lib.json
"angularCompilerOptions": {
"fullTemplateTypeCheck": false,
},
I arrived on this page with the same error message, but resolved this by not using yarn link
when developing Angular libraries.
This is because the default library files are already resolved in the tsconfig.json
file. So in my project angular generated this:
// tsconfig.json
...
"paths": {
"lib-core": [
"dist/lib-core"
],
"lib-core/*": [
"dist/lib-core/*"
],
Then I just run 2 terminals while I'm developing the library package and the app package (which uses the library)
ng build --project=lib-core --watch
in the backgroundng serve --project=my-app
for developing my app
So I don't need yarn link
and I don't get that error message!