Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Angular’s NG0204 means it could not resolve a dependency while constructing a class. Start with the exact error text: in Can't resolve all parameters for X: (?, ?, ?), each question mark marks a constructor parameter Angular could not resolve. Check the corresponding parameter for missing injectable metadata, a missing runtime token, an unavailable provider, or a different InjectionToken instance at the provider and injection sites.

What Angular NG0204 means

NG0204 is Angular’s “Invalid Injection Token” error. It occurs when Angular cannot resolve a dependency for a class during dependency injection. The reported class is the one Angular was trying to construct; inspect its constructor and trace backward through the stack trace to find what triggered that construction.

The wording narrows the search. In Can't resolve all parameters for X: (?, ?, ?), match each ? to the constructor argument in that position. If the message instead says Token X is missing a ɵprov definition, Angular is saying the token lacks a provider definition. Register a provider for it, or give the token a suitable default factory.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Diagnose the failing dependency

  1. Map the error to a constructor parameter

    Find the class named in the error and inspect the parameters in its constructor in order. Focus first on each position marked with ?; those are the unresolved arguments.

  2. Check the class’s injectable metadata

    If Angular needs to construct a class that has constructor dependencies, ensure it has the appropriate @Injectable() metadata. A class with dependencies but without the required injectable metadata can prevent Angular from resolving its constructor.

  3. Confirm the parameter has a runtime token

    Angular needs a runtime value it can use as a dependency token. TypeScript interfaces and other type-only forms disappear after compilation, so they cannot identify a dependency on their own. For interface-shaped values, functions, arrays, and parameterized types, define an InjectionToken and use it to provide and inject the value.

  4. Verify that a provider is available

    A valid token must also have a provider available to the consumer unless it is configured with an appropriate factory or otherwise provided automatically. Check whether the provider is registered in a scope visible to the class that requests it. Angular supports automatic provision through @Injectable or a token factory, as well as manual registration in provider arrays.

    Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  5. Check that provider and consumer use the same token object

    An InjectionToken description is for debugging; Angular uses the token object’s identity. Define and export a token once in a shared file, then import that same instance wherever it is provided or injected. Creating two tokens with identical descriptions still creates two distinct tokens.

Use an InjectionToken for values without a runtime type

For interface-shaped configuration or another value without a reified runtime representation, define one shared token and use it at both ends. The generic type parameter provides TypeScript type information; the token object provides the runtime identity Angular needs.

import { InjectionToken } from '@angular/core';

export interface AppConfig {
  apiUrl: string;
}

export const APP_CONFIG = new InjectionToken<AppConfig>('app.config');

Register a provider for that token in a scope available to the consumer. Choose the provider strategy based on what the dependency represents:

  • useValue supplies a fixed value, such as a configuration object.
  • useClass tells Angular which class to construct.
  • useFactory creates the dependency through a factory function.
  • useExisting aliases another provider.

At the injection site, request the same exported token—for example, with @Inject(APP_CONFIG) on a constructor parameter. Do not create a second InjectionToken with the same description for the provider or consumer.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Why common attempted fixes may not work

Adding @Optional() does not create a token

@Optional() can make an otherwise valid dependency optional when no provider is found. It does not give an interface a runtime identity, register a provider, or make two separately created tokens equivalent.

Registering the provider under a different token does not satisfy the request

Provider and consumer must refer to the same token object. Matching text in their descriptions is not enough.

Calling inject() later is a different problem

Angular permits inject() during construction, field initialization, and provider or token factory execution. Calling it later, such as from a lifecycle hook, is outside the injection context and is associated with NG0203, not NG0204. Move the call into a supported context when the reported code is NG0203.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Distinguish NG0204 from related Angular DI errors

  • NG0204 — Invalid Injection Token: Angular cannot resolve a dependency while constructing a class. Inspect the constructor, runtime token, provider, and token identity.
  • NG2003 — Missing Token: Angular has no runtime token for a constructor parameter, often because the parameter uses a primitive type. Use an appropriate runtime token, commonly an InjectionToken for a value that has no runtime class representation.
  • NG0203 — Injection Context: inject() was called outside a supported injection context. Fix where the call occurs rather than changing provider registration unless a separate provider problem is also present.

Use the code Angular actually reports to choose the correction; these errors can appear related, but they identify different failure conditions.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.