NG0203 Angular Origin: On your device Angular 14+ Last updated 15 Oct 2026

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.

Exact message you may see NG0203: inject() must be called from an injection contextinject() must be called from an injection contexteffect() can only be used within an injection context
Seen on: Angular TypeScript

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 or setTimeout
  • Called after an await in an async function
  • Used in a plain helper function not invoked during construction
  • Test code calling a function that uses inject() outside TestBed.runInInjectionContext
  • Called in a module-level variable at import time

⚡ Quick fix

  1. Move inject() to a field initialiser or the constructor
  2. Wrap in runInInjectionContext(this.injector, () => ...)
  3. In tests use TestBed.runInInjectionContext

Detailed fix by platform

Angular

  1. private http = inject(HttpClient); as a class field instead of inside a method
  2. Keep a reference: private injector = inject(Injector); then runInInjectionContext(this.injector, () => inject(Foo))

Code examples

Field initialiser vs method

typescript
@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

  1. Location — Where is inject() called?
  2. Timing — After await or in a callback?
  3. 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:

Where this error sits

Origin: On your device. Errors that are often confused with or caused by this one (click to open):

NG0200 — Angular: NG0200: Circular dependency in DI detected for X (Angular)NG0200NG0201 — Angular: NG0201: No provider for X found in NodeInjector (Angular)NG0201NG0600 — Angular: NG0600: Writing to signals is not allowed in a computed or an effect by default (Angular)NG0600NG0950 — Angular: NG0950: Input is required but no value is available yet (input.required / viewChild.required) (Angular)NG0950NullInjectorError — Angular: NullInjectorError: No provider for X! (Angular)NullInjectorErrorNullInjectorError: No provider for ActivatedRoute! — Angular: NullInjectorError: No provider for ActivatedRoute! (also Router) — usually a unit test (Angular)NullInjectorError: No…NullInjectorError: No provider for HttpClient! — Angular: NullInjectorError: No provider for HttpClient! — provideHttpClient() missing (Angular)NullInjectorError: No…Schema validation failed with the following errors — Angular CLI: Schema validation failed with the following errors: Data path "" must NOT have additional properties(xyz) (Angular)Schema validation fai…NG0203

Explore the map of this error Full error map →

🧠 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.