I am upgrading Angular on my project and I have faced this issue. Currently I am able to start my project locally successfully, however when I try to deploy it and start it through the server I get an internal server error. The build completes and launches my project on port but when I open it through a browser the following error is thrown:
Error: inject() must be called from an injection context
at injectInjectorOnly (/Users/vasko/Downloads/PROJECT/client/dist/server.js:198150:15)
at ɵɵinject (/Users/vasko/Downloads/PROJECT/client/dist/server.js:198160:57)
at Object.ApplicationModule_Factory [as factory] (/Users/vasko/Downloads/PROJECT/client/dist/server.js:226795:130)
at R3Injector.hydrate (/Users/vasko/Downloads/PROJECT/client/dist/server.js:677770:35)
at R3Injector.get (/Users/vasko/Downloads/PROJECT/client/dist/server.js:677591:33)
at /Users/vasko/Downloads/PROJECT/client/dist/server.js:677628:55
at Set.forEach (<anonymous>)
at R3Injector._resolveInjectorDefTypes (/Users/vasko/Downloads/PROJECT/client/dist/server.js:677628:31)
at new NgModuleRef$1 (/Users/vasko/Downloads/PROJECT/client/dist/server.js:690724:26)
at NgModuleFactory$1.create (/Users/vasko/Downloads/PROJECT/client/dist/server.js:690778:16)
It seems to be some server side rendering issue. I have spent a lot of time trying to resolve this but I have not come to any usefull fix.
I have tried:
- Adding
"preserveSymlinks": true
in myangular.json
file undeerprojects.project-name.architect.build.options
. The same error occurs again. - Adding
"@angular/*": ["../../node_modules/@angular/*"]
and"@angular/*": ["./node_modules/@angular/*"]
in undercompilerOptions.paths
in mytsconfig.app.json
. The same error occurs again. - Adding
"enableIvy": false
under"angularCompilerOptions"
intscofing.app.json
. A bunch of HTML errors occurred but the same internal error occurred once I solved the HTML ones. - Upgrading to Angular 10, the same error occurs.
- Commenting out all of the services in
app.module.ts
. The same error occurs. - Tried adding @Injectable above each component which has @Inject in its constructor. Read about this from here, but as expected the same error occurred. Angular components are injectable by default.
Most of these solutions came from this SO Question and this Github Issues Request
Does anyone have any idea what might be causing this error? I can immediately edit the question and post my files if they are required.