Custom angular library component displays error 'not a known element'

1.4k views Asked by At

I'm trying to create a custom angular library to be used in multiple projects and will be published to a private verdaccio npm registry.

And it works and renders fine and builds fine, but there is this irritating error in vs code which is 'my-component' is not a known element.

I've checked that i'm building in production mode and ivy is disabled and that i'm exporting all the components from the library module and it generates the metadata.json file successfully.

I have been trying to get rid of this error for so long and the closest thing I got is to add

    schemas: [
      CUSTOM_ELEMENTS_SCHEMA
    ]

to my my ngModule.

my cystom module files are:

ng-package.json


{
  "$schema": "../../../node_modules/ng-packagr/ng-package.schema.json",
  "dest": "../../../dist/my-lib",
  "deleteDestPath": false,
  "lib": {
    "entryFile": "src/public-api.ts",
    "umdModuleIds": {}
  },
  "whitelistedNonPeerDependencies": ["tslib"]
}

tsconfig.lib.prod.json


{
  "extends": "./tsconfig.lib.json",
  "compilerOptions": {
    "declarationMap": false
  },
  "angularCompilerOptions": {
    "enableIvy": false
  }
}

public-api.ts

export * from './lib/mylib.module';
export * from './lib/interfaces';
export * from './lib/services/mylib-util.service';
export * from './lib/services/mylib-config.service';
export * from './lib/components/my-component.component';
export * from './lib/components/my-other-component.component';

mylib.module.ts

export function configFactory() {
  return new ConfigService({});
}

@NgModule({
  declarations: [
    MyComponentComponent,
    MyOtherComponent
  ],
  imports: [
    CommonModule,
    FormsModule,
    ReactiveFormsModule
  ],
  providers: [
    UtilService,
    {
      provide: ConfigService,
      useFactory: configFactory
    }
  ],
  exports: [
    MyComponentComponent,
    MyOtherComponentComponent
  ]
})
export class LibraryModule {
  static config(conf: ILibModuleConfig): ModuleWithProviders<LibraryModule> {
    const config = new ConfigService(conf);
    return {
      ngModule: LibraryModule,
      providers: [
        {
          provide: ConfigService,
          useValue: config
        }
      ]
    };
  }
}

I really don't know what I'm missing here!

I've tried modifying the builder in angular.json and used both @angular-devkit/build-ng-packagr:build and @angular-devkit/build-angular:ng-packagr and both gave me the same result.

my devDependencies

"devDependencies": {
    "@angular-devkit/build-angular": "~0.1001.3",
    "@angular-devkit/build-ng-packagr": "^0.1001.3",
    "@angular/cli": "~10.1.2",
    "@angular/compiler-cli": "~10.1.2",
    "@types/jasmine": "~3.5.0",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "^12.11.1",
    "codelyzer": "^6.0.0",
    "jasmine-core": "~3.6.0",
    "jasmine-spec-reporter": "~5.0.0",
    "karma": "~5.0.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage-istanbul-reporter": "~3.0.2",
    "karma-jasmine": "~4.0.0",
    "karma-jasmine-html-reporter": "^1.5.0",
    "ng-packagr": "^10.1.0",
    "protractor": "~7.0.0",
    "ts-node": "~8.3.0",
    "tsickle": "^0.39.1",
    "tslint": "~6.1.0",
    "typescript": "~4.0.2"
  }
1

There are 1 answers

0
Sayed Mahmoud Sayed On BEST ANSWER

after a lot of finagling with this, everything worked as it should when I rolled back the version of ng-packagr to version 9.1.5 instead of version 10.1.0