I am currently upgrading our application from Angular v15 to v18. Also, our application is in transition to become a fully standalone application.
I have switched from ngModule to bootstrapApplication.
Here's the error when using APP_INITIALIZER with HTTP_CLIENT:
core.mjs:6467 ERROR Error: 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. Find more at https://angular.dev/errors/NG0203 at injectInjectorOnly2 (core.mjs:1091:11) at ɵɵinject2 (core.mjs:1101:40) at Object.HttpClient_Factory [as factory] (http.mjs:1459:39) at core.mjs:3132:35 at runInInjectorProfilerContext (core.mjs:866:5) at R3Injector.hydrate (core.mjs:3131:11) at R3Injector.get (core.mjs:3005:23) at injectInjectorOnly (core.mjs:1095:36) at ɵɵinject (core.mjs:1101:40) at injectArgs (core.mjs:1226:17)
This error arise when importing code from our library with regards to HTTP_CLIENT:
export function provideCommonLib(): EnvironmentProviders { return makeEnvironmentProviders([ provideHttpClient(), { provide: APP_INITIALIZER, useFactory: () => { console.log('APP_INITIALIZER LIB'); return () => true; }, deps: [HttpClient], multi: true } ]); } bootstrapApplication(AppComponent, { providers: [ provideHttpClient(), { provide: APP_INITIALIZER, useFactory: () => { console.log('APP_INITIALIZER APP'); return () => true; }, deps: [HttpClient], multi: true }, provideCommonLib() ] }).then(); I have added the same code in our application and the factory method for APP_INITIALIZER is being called without throwing any error. But the code in the library throws an error. When I remove the deps: [HttpClient], the code is executed. HttpClient is needed to load some code on application launch.
Any ideas what is missing? The added code is following the example from Angular doc directly: https://angular.dev/api/core/APP_INITIALIZER?tab=usage-notes.
Note: the code is obviously stripped down to understand where the issue arise.
APP_INITIALIZER. I added it temporarily while troubleshooting and it shows that it works in the application while throwing an exception in the library. Having two should not be an issue since I addedmulti: true. Correct, only the one from the library is throwing an exception.HttpInterceptorthat is interfering?