Sub topic 13
Common pitfalls and debugging NullInjectorError
This chapter assumes you know how providers, inject(), environment injectors and element injectors work, as covered earlier in this tutorial. It focuses on the failures you will actually see and how to trace them.
Why lookups fail
Angular searches upward only. A request starts at the requesting node's injector and moves to its ancestors. It never searches children, siblings or other routes. Almost every error in this area comes from a provider sitting in a place the search never visits.
When the search reaches the end without a match, Angular throws NullInjectorError: No provider for X. The message includes a path showing which injector started the search and which token was missing.
Pitfall 1: no provider anywhere
A service with a plain @Injectable() decorator is not registered anywhere.
import { Injectable } from '@angular/core';
@Injectable()
export class ReportService {
load(): string[] {
return [];
}
}Injecting ReportService anywhere fails. Pick the scope you need. Use @Injectable({ providedIn: 'root' }) for an application-wide singleton. Otherwise list the class in providers on a component, a route or bootstrapApplication.
Pitfall 2: the provider is in a branch the lookup cannot reach
A service listed in a route's providers lives in that route's environment injector. A root service cannot see it, because root is an ancestor of the route injector and lookups go upward only.
import { Injectable, inject } from '@angular/core';
import { AdminSettingsService } from './admin-settings.service';
@Injectable({ providedIn: 'root' })
export class AuditService {
// Throws NullInjectorError if AdminSettingsService is only provided on a route.
private readonly settings = inject(AdminSettingsService);
}Move the provider up to a scope that both consumers can reach, or stop depending on the narrower service from the wider one.
Pitfall 3: unexpected extra instances
If you list a service in a component's providers, every instance of that component gets its own copy. The same happens if you provide a service both at root and on a route. The route's consumers get the route copy, and everyone else gets the root copy. Two parts of the app then look at different state, and nothing throws an error.
Pitfall 4: projected content and viewProviders
A service listed in viewProviders is hidden from projected content, so ng-content children cannot inject it. A child projected into the component that expects the service gets NullInjectorError or falls through to an outer instance. Use providers if projected content needs to reach it.
Debugging checklist
- Read the resolution path in the error message to see where the search began.
- Search the codebase for the token in
providers,viewProvidersand route configuration. - Check each
inject()call forself,skipSelforhost, which change where the search starts or stops. - If two parts of the app disagree, look for a provider declared more than once.
Exercise
A component lists CartService in its providers. A sibling component, not a descendant, injects CartService and throws NullInjectorError, although CartService has no providedIn. Why, and what is one fix?
Show answer
The provider sits in the first component's element injector, and lookups only go upward, so the sibling cannot reach it. Fix it by providing CartService in a shared ancestor component, a common route, or with providedIn: 'root'.