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:
- A service with no registration. A class marked with plain
@Injectable()is not provided anywhere. Fix:@Injectable({ providedIn: 'root' }), or add it toprovidersof a component, route or the application config. HttpClientwithout setup. In standalone apps addprovideHttpClient()to the application config. Without it:No provider for HttpClient.- Router tokens (
ActivatedRoute,Router) in a unit test. AddprovideRouter([])to the test providers. - Providers in the wrong place. A service listed in the
providersof one component is visible to that component and its children, not to a sibling or a parent. - An
InjectionTokenthat has no value and no default factory. Provide it, or give the token afactory. - A library service that needs a provider function, for example
provideAnimations(),provideStore()for NgRx, orprovideNativeDateAdapter()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.
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.