Why do i keep getting the NG0203 error when i build my Angular app using esbuild?
Full error message
Im trying to migrate to esbuild to build my Angular application. I've completed everything i needed to do, but i keep seeing this error being logged in the console:
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
I've tried everything thats described here, at 'Using application builder': https://angular.io/guide/esbuild
My app.config.ts looks like this:
export const appConfig: ApplicationConfig = {
providers: [
importProvidersFrom(
BrowserModule,
SharedModule,
DataSetModule,
LookUpTablesModule,
NormsModule,
StoreModule.forRoot({
mappings: mappingsReducer,
auth: authReducer,
}),
EffectsModule.forRoot([AuthEffects])
),
provideAnimations(),
provideHttpClient(),
provideRouter(routes),
],
};
And i inject classes like this, which worked with the former builder:
constructor(
private router: Router,
public excelDownload: ExcelDownloadService,
private route: ActivatedRoute,
private dataSetService: DataSetService
) {} ```
Edit: this is the exact error that is being logged:
`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.io/errors/NG0203
at injectInjectorOnly (core.mjs:985:15)
at ɵɵinject3 (core.mjs:998:42)
at Object.factory (core.mjs:27529:46)
at core.mjs:6247:43
at runInInjectorProfilerContext (core.mjs:948:9)
at R3Injector.hydrate (core.mjs:6246:17)
at R3Injector.get (core.mjs:6114:33)
at definition.getStandaloneInjector (core.mjs:27545:31)
at ComponentFactory.create (core.mjs:15477:57)
at _ApplicationRef.bootstrap (core.mjs:31863:42)`Solutionsource: stackoverflow \u2197
If you see this error while SWITCHING to ESBUILD, you probably have a @angular path in your tsconfig. REMOVE this option now, and it will fix the issue. "paths": { "@angular/*": ["./node_modules/@angular/*"], <--- DELETE THIS ... } Context: This path was likely added to workaround an Angular issue when developing with a locally-linked library. There would be two Ivy instances and this path would tell your app which node_modules to read from. At least that's what one popular guide said, and it seemed to fix the issue. I don't believe this is an issue anymore with esbuild. At least I haven't run into it, so it should be okay to remove this path.
API access
Get this solution programmatically \u2014 free, no authentication.
curl https://depscope.dev/api/error/ca8e092286015bd8908002d23b3f4c044df9eebb7d4eb3107eefd10f6a9661c6hash \u00b7 ca8e092286015bd8908002d23b3f4c044df9eebb7d4eb3107eefd10f6a9661c6