This error occurs when EnvironmentProviders are used in a context that only accepts regular providers, such as a component's providers array. Environment providers are designed for application-wide configuration and can only be used in environment injectors (like the root injector configured in bootstrapApplication or route configurations).
Functions like provideHttpClient() return EnvironmentProviders, which cannot be used at the component level:
@Component({
providers: [
provideHttpClient(), // ERROR: EnvironmentProviders can't be used here
],
})
export class UserProfile {}
The importProvidersFrom() function also returns EnvironmentProviders:
Move the environment providers to an appropriate location:
Configure environment providers in bootstrapApplication:
Use the providers array in route configurations:
If you need component-scoped services, use regular providers instead of environment providers:
@Component({
providers: [
UserClient, // Regular provider - this works
{provide: API_URL, useValue: '/api'}, // Value provider - this works
],
})
export class UserProfile {}