Angular: NG0203: inject() must be called from an injection context such as a constructor, a factory function, a field initializer or a function used with runInInjectionContext
inject() was called somewhere Angular is not constructing a class — for example inside a method, a callback or after an await.
NG0203: inject() must be called from an injection contextinject() must be called from an injection contexteffect() can only be used within an injection context Meaning
inject() only works while Angular is creating something (constructor, field initialiser, provider factory, guards/resolvers). Calling it later, in ngOnInit after async work, in setTimeout, or in plain functions throws NG0203. Inject once as a field and reuse, or run the code in runInInjectionContext.
Common causes
inject()called inside a method,ngOnInit, a subscribe callback orsetTimeout- Called after an
awaitin an async function - Used in a plain helper function not invoked during construction
- Test code calling a function that uses
inject()outsideTestBed.runInInjectionContext - Called in a module-level variable at import time
⚡ Quick fix
- Move
inject()to a field initialiser or the constructor - Wrap in
runInInjectionContext(this.injector, () => ...) - In tests use
TestBed.runInInjectionContext
Detailed fix by platform
Angular
private http = inject(HttpClient);as a class field instead of inside a method- Keep a reference:
private injector = inject(Injector);thenrunInInjectionContext(this.injector, () => inject(Foo))
Code examples
Field initialiser vs method
@Component({ selector: 'app-x', standalone: true, template: '' })
export class XComponent {
private http = inject(HttpClient); // OK: field initialiser
load() {
// const router = inject(Router); // NG0203: not an injection context
this.http.get('/api').subscribe();
}
}How to diagnose
- Location — Where is inject() called?
- Timing — After await or in a callback?
- Tests — Using TestBed.runInInjectionContext?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
Most viewed in Angular
- NG0100 Angular: NG0100: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked
- NG8002 Angular: NG8002: Can't bind to 'ngModel' since it isn't a known property of 'input'
- Cannot GET /route on refresh Angular (production): Cannot GET /dashboard — 404 when refreshing or opening a deep link
Other ways to find it
Where this error sits
Origin: On your device. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 15 Oct 2026