Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Angular throws NG0203 because inject() runs outside an injection context

ErrorNG0203: inject() must be called from an injection context

Short answer

NG0203 means inject() ran at a moment when Angular was not creating anything. It only works in a constructor, a field initializer, a provider or token factory, or a function called from one of those. Move the call to a field or constructor, wrap it in runInInjectionContext, or in tests use TestBed.runInInjectionContext.

  • Applies to Angular 19, Angular 20
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read the stack trace from the top and find the first line in your own code. That line is the inject() call (or a function that calls it), and where it sits decides the cause.

    ng serve
    # then open the browser console and read the NG0203 stack trace
  2. 2Match what you see, then jump to its fix

    1. 1The stack trace points to ngOnInit, an event handler, a subscribe callback or another method of the classinject() is called inside a method or a lifecycle hookFix
    2. 2The stack trace shows your own function (for example a utility or a function used in a callback) that calls inject(), called from a timer, a promise or a plain methodA helper function that calls inject() is run from outside any injection contextFix
    3. 3NG0203 appears only in the spec, from a function or factory you call by handA unit test calls code that uses inject() directlyFix

What does the error look like?

  • NG0203: inject() must be called from an injection context

Cause 1 of 3

inject() is called inside a method or a lifecycle hook

1Check

Angular's NG0203 page lists the places where inject() is allowed: constructors, constructor parameters, field initializers and provider factories. A method runs after the instance exists, which the page calls too late.

grep -rn "inject(" src/app | grep -v "= inject("

2Fix

Call inject() once in a field initializer and use the stored value inside methods.

export class ProfileComponent {
  private http = inject(HttpClient);

  ngOnInit() {
    this.http.get('/api/me').subscribe();
  }
}

3Verify

Reload the page and confirm the console no longer shows NG0203.

ng build

Confirmed by angular.dev

Did this fix it?

Cause 2 of 3

A helper function that calls inject() is run from outside any injection context

1Check

The Angular dependency-injection context guide says inject() works in a stack frame that runs within an injection context, such as a router guard. A helper called later, from a method, has no such context.

grep -rn "function .*inject\|runInInjectionContext" src/app

2Fix

Inject an EnvironmentInjector (or Injector) in a field initializer, and run the helper through runInInjectionContext. As an alternative, move the inject() call into the field initializer and pass the result in as an argument.

import { EnvironmentInjector, inject, runInInjectionContext } from '@angular/core';

export class ReportService {
  private injector = inject(EnvironmentInjector);

  run() {
    runInInjectionContext(this.injector, () => {
      inject(SomeService);
    });
  }
}

3Verify

Trigger the code path that failed and confirm no NG0203 is thrown.

ng test

Confirmed by angular.dev

Did this fix it?

Cause 3 of 3

A unit test calls code that uses inject() directly

1Check

Outside Angular's own creation of a class, a test has no injection context. The NG0203 page names TestBed.runInInjectionContext as the way to supply one.

grep -rn "inject(" src --include=*.spec.ts

2Fix

Run the call inside TestBed.runInInjectionContext, after configuring the testing module.

it('reads the service', () => {
  const result = TestBed.runInInjectionContext(() => myInjectingFunction());
  expect(result).toBeDefined();
});

3Verify

Run the spec again.

ng test

Confirmed by angular.dev

Did this fix it?

How do I stop it coming back?

  • Declare dependencies as fields (private http = inject(HttpClient)) so they are created at construction time.
  • In helper functions that use inject(), call assertInInjectionContext(theFunction) first, so a wrong call gives an error that names your helper.
  • Keep inject() out of callbacks; read the dependency into a variable before the callback starts.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Related fixes

Back end

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 causes, each with a fix

Back end

ng serve cannot find the dev-server builder package

Could not find the implementation for builder @angular/build:dev-server

The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.