# 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

**Code:** `NG0203` · **Category:** Angular · **Seen on:** Angular, TypeScript

`inject()` was called somewhere Angular is not constructing a class — for example inside a method, a callback or after an `await`.

## 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?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/angular/ng0203-inject-outside-injection-context)
