Header Ads

Fix NullInjectorError: No provider for HttpClient in Angular and Ionic. Standalone, NgModule, Ionic Storage and unit test fixes with code.

Seeing “NullInjectorError: No provider for HttpClient!” in your Angular or Ionic app? This guide explains what the error means and shows 6 fixes for standalone apps, NgModule apps, Ionic Storage and unit tests, with code you can copy.

You add an API service, inject HttpClient, run the app, and the browser console shows this:

ERROR NullInjectorError: R3InjectorError(AppModule)[ApiService -> HttpClient -> HttpClient]:
NullInjectorError: No provider for HttpClient!

In a standalone app, the first part looks like R3InjectorError(Standalone[AppComponent]) instead of AppModule. The app compiles fine, and the error only appears when the app runs or when a test starts.

What Does “No provider for” Mean?

Angular uses dependency injection. When a class asks for something in its constructor, such as HttpClient, Angular looks for a provider that tells it how to create that object. “No provider” means Angular searched every injector it could reach and found no recipe.

The part in square brackets shows the chain of requests. In the example above, ApiService asked for HttpClient, and nobody had registered it. Read that chain first, because it names the exact class that is missing.


Step 1: Find Out What Is Missing

Look at the last name in the chain, after No provider for. The most common cases are:

MessageUsual causeGo to
No provider for HttpClient!HTTP support was never registeredStep 2
No provider for ApiService! (your own class)The service is not injectable or not providedStep 4
No provider for Storage!Ionic Storage module not set upStep 5
Error appears only when running testsThe test setup lacks the providersStep 6
Works in one page, fails in a lazy-loaded oneHttpClient provided in the wrong placeStep 7


Step 2: Provide HttpClient

Standalone apps (no AppModule, which is the default in new Angular and Ionic projects). Add provideHttpClient() to the providers in main.ts or app.config.ts:

import { bootstrapApplication } from '@angular/platform-browser';
import { provideHttpClient } from '@angular/common/http';
import { AppComponent } from './app/app.component';

bootstrapApplication(AppComponent, {
providers: [
// keep your existing providers, such as provideRouter(...)
provideHttpClient()
]
});

For Ionic standalone projects, keep the providers you already have, for example provideIonicAngular() and provideRouter(routes), and add provideHttpClient() next to them.

NgModule apps (with an AppModule). Import the HTTP module once in the root module:

import { HttpClientModule } from '@angular/common/http';

@NgModule({
declarations: [AppComponent],
imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule, HttpClientModule],
bootstrap: [AppComponent]
})
export class AppModule {}

In Angular 15 and newer you can also use provideHttpClient() in the providers array of an NgModule, and newer versions recommend it over HttpClientModule. If you use interceptors, they are registered in the same call with provideHttpClient(withInterceptors([...])).


Step 3: Check the Import Path

A very common copy-paste mistake is importing from the wrong package. Make sure your service imports HttpClient from the right place:

// correct
import { HttpClient } from '@angular/common/http';

// wrong: this is an old, removed package
import { Http } from '@angular/http';

Old tutorials from the Ionic 3 and Angular 4 era use @angular/http. If you copied code from one of them, replace it with HttpClient from @angular/common/http.


Step 4: Make Your Own Services Injectable

If the missing name is your own class, Angular does not know it exists. The simplest fix is to register it at the root:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable({ providedIn: 'root' })
export class ApiService {
constructor(private http: HttpClient) {}

getPing() {
return this.http.get('/api/ping');
}
}

Without providedIn: 'root' (or an entry in a providers array), Angular cannot create the service. Also check that you did not list the service in providers of a different module or component than the one that uses it.


Step 5: Set Up Ionic Storage and Other Ionic Providers

Ionic Storage needs its own setup. Without it, you get No provider for Storage!. In an NgModule app:

import { IonicStorageModule } from '@ionic/storage-angular';

@NgModule({
imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule, IonicStorageModule.forRoot()]
})
export class AppModule {}

In a standalone app, wrap the module with importProvidersFrom:

import { importProvidersFrom } from '@angular/core';
import { IonicStorageModule } from '@ionic/storage-angular';

bootstrapApplication(AppComponent, {
providers: [
importProvidersFrom(IonicStorageModule.forRoot())
]
});

If you followed an Ionic 3 tutorial that lists plugins like Camera or Device in a providers array, note that Capacitor plugins do not need providers at all. They are imported and called directly.


Step 6: Fix the Error in Unit Tests

Your app may run fine while ng test fails with the same message. Tests start with an empty injector, so you must provide what the component needs. For HTTP, use the testing providers:

import { TestBed } from '@angular/core/testing';
import { provideHttpClient } from '@angular/common/http';
import { provideHttpClientTesting } from '@angular/common/http/testing';

beforeEach(() => {
TestBed.configureTestingModule({
providers: [provideHttpClient(), provideHttpClientTesting()]
});
});

On older Angular versions, import HttpClientTestingModule in the imports array of configureTestingModule instead. The same idea applies to the router: if a test reports No provider for ActivatedRoute, add provideRouter([]) to the test providers.


Step 7: Do Not Provide HttpClient Twice

In NgModule apps, importing HttpClientModule inside a lazy-loaded module creates a second, separate HttpClient. It will not share the interceptors you registered in the root, so your auth header can silently disappear on some pages. Import it only once, in the root module, and remove it from feature modules.

In standalone apps, avoid importing HttpClientModule inside individual components. Provide HTTP once, at bootstrap, as shown in Step 2.


Quick Fix Table

Where the error appearsFix
Whole app, standaloneprovideHttpClient() in main.ts or app.config.ts
Whole app, with AppModuleHttpClientModule or provideHttpClient() in the root module
Your own service@Injectable({ providedIn: 'root' })
Ionic StorageIonicStorageModule.forRoot()
Only in testsprovideHttpClient() and provideHttpClientTesting()
Auth header missing on some pagesRemove duplicate HttpClientModule imports


Best Practices

  • Provide shared services such as HttpClient once, at the root, and not in every component.
  • Use providedIn: 'root' for services that the whole app uses.
  • Keep one place for app-wide providers, such as app.config.ts, so you always know where to look.
  • After upgrading Angular or Ionic, check that your providers still match the new recommended style.
  • Write a small service test early. It reveals missing providers before users do.


Conclusion

A NullInjectorError is Angular telling you it has no recipe for something your code asked for. Read the chain in the message, find the last name, and register it in the right place: provideHttpClient() for HTTP, providedIn: 'root' for your own services, and the matching providers in your tests. Once you know where to look, it takes only a minute to fix.

Related Guides

Post a Comment

0 Comments