1

Problem:

I work in angular workspace and I import common.module.ts from library (library generated with ng generate library) to my app.module.ts, but when I inject CommonService in app.component.ts I get error:

main.ts:11 ERROR NullInjectorError: R3InjectorError(AppModule)[CommonService -> CommonService -> CommonService]: NullInjectorError: No provider for CommonService! 

There is an provider for CommonService in common.module.ts so I expect when I inject service in component no error should occure.

Code:

Library module common.module.ts
import { NgModule } from '@angular/core'; import { CommonComponent } from './common.component'; import { HttpClientModule } from '@angular/common/http'; import { CommonService } from './common.service'; @NgModule({ declarations: [], imports: [], exports: [], providers: [ CommonService ] }) export class CommonModule { } 
Application module app.module.ts
import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { AppComponent } from './app.component'; import { HttpClientModule } from '@angular/common/http'; import { CommonModule } from 'common'; @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, HttpClientModule, CommonModule ], providers: [ ], bootstrap: [AppComponent] }) export class AppModule { } 
app.component.ts
import { Component } from '@angular/core'; import { CommonService } from '../../../common/src/lib/common.service'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.less'] }) export class AppComponent { constructor(private common2: CommonService) { } } 

common.service.ts

@Injectable() export class CommonService { constructor() { this.initProps(); } } 
9
  • With your sample code, I'm able to use CommonService without problems. Can you provide an example on stackblitz ? Commented Jan 13, 2023 at 17:15
  • Is common.module.ts located inside library (in your case)? Because this error only reproduced in case I import common.module.ts from library (library generated using cli ng generate library) Commented Jan 16, 2023 at 8:34
  • Try to add it in providers. I think that injection token is different in both cases. Commented Jan 16, 2023 at 10:25
  • @Antoniossss, injection token is the same (name class). But your solution works. I still have a question: why couldn't service imported from library be injected? Does library have some restrictions that prevents injecting service listed in providers array of library module? Commented Jan 16, 2023 at 11:11
  • 1
    @VladyslavZhadchenko i think I know what is wrong. You are not using public-api for your imports. Your import should look like "import X from 'lib-name" just like for RectiveFormsModule you have mentioned;) Commented Jan 17, 2023 at 9:45

1 Answer 1

3

You are using different injection token then the one used in the module since your are importing sources directly with

import { CommonService } from '../../../common/src/lib/common.service'; 

expose it via public-api in your library, then use it via public api just like you do with the module

import { CommonModule } from 'common'; 

eg

import { CommonService } from 'common'; 

and then it will work.

Right now you are expecting compiled entity in the library to be equal to entity that is compiled directly into your applicaiton, which will not work (class in the module is not the same class you are importing(compiling it again))

Sign up to request clarification or add additional context in comments.

2 Comments

After ng build library and using import { CommonModule } from 'common' it finally works. Thank you!
This saved my day... I had a similar problem while using an InjectionToken in my library. I had made a mistake of exporting the file from 2 entrypoints (root and then a /shared public-api). The component in my library was pointing to the file in /shared and the app loading the component was pointing to the root level file (the import was made automatically by the IDE)... This had me scratching my head for a few hours... Your answer pointed me in the right direction, thanks !

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.