2

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:

  1. Adding "preserveSymlinks": true in my angular.json file undeer projects.project-name.architect.build.options. The same error occurs again.
  2. Adding "@angular/*": ["../../node_modules/@angular/*"] and "@angular/*": ["./node_modules/@angular/*"] in under compilerOptions.paths in my tsconfig.app.json. The same error occurs again.
  3. Adding "enableIvy": false under "angularCompilerOptions" in tscofing.app.json. A bunch of HTML errors occurred but the same internal error occurred once I solved the HTML ones.
  4. Upgrading to Angular 10, the same error occurs.
  5. Commenting out all of the services in app.module.ts. The same error occurs.
  6. 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.

Vasko Vasilev
  • 554
  • 1
  • 10
  • 25
  • When you upgrade you are bound to encounter various issues with typescript. Your best bet is to solve one item at a time until all the errors are resolved. Also why would you deploy an app that is not even building locally? – Owen Kelvin Apr 25 '21 at 13:35
  • I may have worded my question poorly. It's working when I start it locally using ng serve, but it fails when I try to deploy it on a server and start it from there. – Vasko Vasilev Apr 25 '21 at 13:53
  • Are you able to run `ng build --aot --prod` successfully? – Owen Kelvin Apr 25 '21 at 14:10
  • I made some changes to angular.json and managed to build it without those warnings. – Vasko Vasilev Apr 25 '21 at 14:34
  • @VaskoVasilev could you share the changes you made? I upgraded a project from angular 11 to angular 12 and am running into the same problem. – Timo Nov 16 '21 at 08:58

0 Answers0