Error: inject() must be called from an injection context when deploying with Angular 9 or Angular 10

Viewed 463

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.

0 Answers
Related