← Back to Angular, page 9

angular-0161 · Easy fresher

What does "NullInjectorError: No provider for X" mean, what are the usual causes, and how do you fix it in an app and in a test?

It means the injector looked for a provider for a token and found none. Older versions print `NullInjectorError: No provider for X!`, and Angular 20 prints `NG0201: No provider found for X`. Typical causes: a service without providedIn or providers, HttpClient used without provideHttpClient(), Router or other framework services missing in a test, or a service provided in a place that the component cannot reach. Provide the token where the consumer can see it.

Full answer

Dependency injection works like this: a class asks for a token (usually a class such as HttpClient), and the injector looks for a provider that knows how to create it. If it climbs to the top and finds nothing, it throws an error. Older versions print NullInjectorError: No provider for HttpClient!. In Angular 20 the text is NG0201: No provider found for HttpClient, followed by the source and the path of the dependency chain, such as StudentApi -> HttpClient. The name NullInjectorError is still what people search for, and the cause and the fixes are the same.

Common causes and fixes:

  1. A service with no registration. A class marked with plain @Injectable() is not provided anywhere. Fix: @Injectable({ providedIn: 'root' }), or add it to providers of a component, route or the application config.
  2. HttpClient without setup. In standalone apps add provideHttpClient() to the application config. Without it: No provider for HttpClient.
  3. Router tokens (ActivatedRoute, Router) in a unit test. Add provideRouter([]) to the test providers.
  4. Providers in the wrong place. A service listed in the providers of one component is visible to that component and its children, not to a sibling or a parent.
  5. An InjectionToken that has no value and no default factory. Provide it, or give the token a factory.
  6. A library service that needs a provider function, for example provideAnimations(), provideStore() for NgRx, or provideNativeDateAdapter() for Material date pickers.

How to read the error: the first line names the token. Ask "who should provide this?" and put the provider at the right level. Use { optional: true } in inject only when the dependency is truly optional.

In tests, TestBed.configureTestingModule({ providers: [...] }) is the place. Prefer the real provider function (provideHttpClient() with provideHttpClientTesting()), or supply a fake: { provide: GradeApi, useValue: fakeApi }.

Do not fix the error by providing the same service in many components, because each place creates a new instance, and shared state will no longer be shared.

typescript
import { Injectable, inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { TestBed } from '@angular/core/testing';
import { provideHttpClient } from '@angular/common/http';

@Injectable() // not provided anywhere
export class BrokenService {}

@Injectable({ providedIn: 'root' }) // fixed: provided at the root
export class FixedService {}

@Injectable({ providedIn: 'root' })
export class StudentApi {
  private readonly http = inject(HttpClient); // needs provideHttpClient()
  load(): void {
    this.http.get('/api/students').subscribe();
  }
}

export function reproduce(): string[] {
  const out: string[] = [];
  try {
    TestBed.inject(BrokenService);
  } catch (error) {
    out.push((error as Error).message.split('\n')[0]);
  }
  try {
    TestBed.inject(StudentApi);
  } catch (error) {
    out.push((error as Error).message.split('\n')[0]);
  }
  TestBed.resetTestingModule();
  TestBed.configureTestingModule({ providers: [provideHttpClient()] });
  out.push(TestBed.inject(FixedService) ? 'FixedService ok' : 'missing');
  out.push(TestBed.inject(StudentApi) ? 'StudentApi ok with provideHttpClient' : 'missing');
  return out;
}

Angular 20.3. A Karma test reproduces the error for an unprovided service and for HttpClient, then shows the fixed forms. The captured messages (Angular 20.3) start with `NG0201: No provider found for` and name the token, and the second one ends with the path `StudentApi -> HttpClient`.

Key points

  • The error means no provider exists for the requested token at the injector level that was searched.
  • Fix by `providedIn: 'root'`, `providers`, or a provider function such as `provideHttpClient()`.
  • In tests add providers in `TestBed.configureTestingModule`.
  • Provide at the right level. Do not copy providers everywhere.

Common mistakes

  • Using `@Injectable()` without `providedIn` or `providers`.
  • Forgetting `provideHttpClient()` or `provideRouter()` in tests.
  • Providing a service in a sibling component and expecting a parent to see it.

Follow-ups

  • What does `{ optional: true }` change?
  • Why is providing in several components risky?
  • How do you replace a real service with a fake in a test?

Tips

  • Read the token name in the message first.
  • Mention provider functions such as `provideHttpClient`.
  • Explain levels: root, route, component.

Evaluation rubric

  • Explains the lookup and the message.
  • Lists the usual causes with fixes.
  • Handles the test setup.