How to fix Angular NullInjectorError: No provider for HttpClient!
In case you see the following error for your Angular application in your JS console:
main.ts:6 ERROR NullInjectorError: R3InjectorError(AppModule)[MyService -> HttpClient -> HttpClient -> HttpClient]:
NullInjectorError: No provider for HttpClient!
at NullInjector.get (core.mjs:7599:27)
at R3Injector.get (core.mjs:8020:33)
at R3Injector.get (core.mjs:8020:33)
at R3Injector.get (core.mjs:8020:33)
at injectInjectorOnly (core.mjs:634:33)
at Module.ɵɵinject (core.mjs:638:60)
at Object.MyService_Factory [as factory] (my.service.ts:8:38)
at R3Injector.hydrate (core.mjs:8121:35)
at R3Injector.get (core.mjs:8009:33)
at ChainedInjector.get (core.mjs:12179:36)
you need to add HttpClientModule
to your app.module.ts
.
First, import HttpClientModule
at the top of app.module.ts
:
import { HttpClientModule } from '@angular/common/http';
After that, add
HttpClientModule,
to the imports: [...]
section of your @NgModule
, for example:
@NgModule({
declarations: [
AppComponent,
],
imports: [
HttpClientModule,
BrowserModule,
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
After that, your error should have disappeared