analogjs / analog

The fullstack meta-framework for Angular. Powered by Vite and Nitro
https://analogjs.org
MIT License
2.59k stars 248 forks source link

RuntimeError: 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` #1032

Closed slashby closed 7 months ago

slashby commented 7 months ago

Please provide the environment you discovered this bug in.

A minimal repo to reproduce the problem:

https://github.com/slashby/analogjs-sample

Which area/package is the issue in?

platform

Description

The following error occurs when import providers from FormlyModel (@ngx-formly/core package) via importProvidersFrom() (app.config.ts) when serve or build app. It is also reproducible for other packages for example when use Maskito directive.

RuntimeError: NG0203: inject() must be called from an injection context such as a constructor, a factory function, a field initializer, or a function used withrunInInjectionContext. Find more at https://angular.io/errors/NG0203

Please provide the exception or error you saw

RuntimeError: 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 (.../sample-app/node_modules/@angular/core/fesm2022/core.mjs:1093:15)
    at ɵɵinject (.../sample-app/node_modules/@angular/core/fesm2022/core.mjs:1106:61)
    at Object.FormlyModule_Factory [as useFactory] (ng:///FormlyModule/ɵfac.js:5:40)
    at Object.factory (.../sample-app/dist/sample-app/.nitro/prerender/chunks/_/renderer.mjs:1663:32)
    at R3Injector.hydrate (.../sample-app/dist/sample-app/.nitro/prerender/chunks/_/renderer.mjs:1591:33)
    at R3Injector.get (.../sample-app/dist/sample-app/.nitro/prerender/chunks/_/renderer.mjs:1513:23)
    at injectInjectorOnly (.../sample-app/dist/sample-app/.nitro/prerender/chunks/_/renderer.mjs:508:36)
    at ɵɵinject (.../sample-app/dist/sample-app/.nitro/prerender/chunks/_/renderer.mjs:513:59)
    at useValue (.../sample-app/dist/sample-app/.nitro/prerender/chunks/_/renderer.mjs:1366:25)
    at R3Injector.resolveInjectorInitializers (.../sample-app/dist/sample-app/.nitro/prerender/chunks/_/renderer.mjs:1545:9)

Other information

No response

I would be willing to submit a PR to fix this issue

brandonroberts commented 7 months ago

Add the @ngx-formly/** entry to the ssr.noExternal array

https://deploy-preview-1029--analog-docs.netlify.app/docs/features/server/server-side-rendering#transforming-packages-for-ssr-compatibility