nx: Error: "inject() must be called from an injection context such as a constructor" when loading remote Angular application from host app
Current Behavior
After migrating NX from 16.2.2 -> 16.8.1 our remote (using module federation) applications failes with this 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`
They work as standalone applications but not remotes. I.e when I go to http://locahost:4201 it loads the dashboard, but not when trying to load it as a route in our host-app.
It might be related to this issue: https://github.com/nrwl/nx/issues/17696 And/or maybe this issue: https://github.com/angular/angular/issues/51532
The module federation is setup according to the guide.
Expected Behavior
It should not fail to run the application as a remote application using module federation.
GitHub Repo
https://github.com/JonWallsten/nx-repro
Steps to Reproduce
- clone repo
- npm i
- npx nx serve host
Or from scratch:
- npx create-nx-workspace debug
- npx nx migrate latest
- npx nx g @nx/angular:host host
- npx nx g @nx/angular:remote remote --host=host
- npx nx serve host
Nx Report
> NX Report complete - copy this into the issue template
Node : 18.14.1
OS : win32-x64
npm : 9.3.1
nx : 16.8.1
@nx/js : 16.8.1
@nx/jest : 16.8.1
@nx/linter : 16.8.1
@nx/workspace : 16.8.1
@nx/angular : 16.8.1
@nx/cypress : 16.8.1
@nx/devkit : 16.8.1
@nx/eslint-plugin : 16.8.1
@nrwl/tao : 16.8.1
@nx/web : 16.8.1
@nx/webpack : 16.8.1
typescript : 5.1.6
Failure Logs
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:731:15)
at Module.ɵɵinject (core.mjs:744:60)
at Object._class6_Factory [as factory] (common.mjs:67:84)
at core.mjs:9412:43
at runInInjectorProfilerContext (core.mjs:694:9)
at R3Injector.hydrate (core.mjs:9411:17)
at R3Injector.get (core.mjs:9281:33)
at injectInjectorOnly (core.mjs:738:40)
at ɵɵinject (core.mjs:744:60)
at inject (core.mjs:828:12)
Package Manager Version
9.3.1
Operating System
- Windows
Additional Information
About this issue
- Original URL
- State: open
- Created 10 months ago
- Reactions: 7
- Comments: 19 (1 by maintainers)
After adding
@angular/coreand@angular/routermanually here before building the remote: https://github.com/nrwl/nx/blob/dde91067f0ddd7bd7199f3727fc19dd4ca1f7c07/packages/angular/src/utils/mf/utils.ts#L109-L118The application works fine again. So that begs the question: Why is
@angular/coreand@angular/routermissing as dependencies for the remote?Edit: I get it now. No dependecies at all is found. Common/Animations are from some kind of constant called
DEFAULT_ANGULAR_PACKAGES_TO_SHARESame output from 16.2.2:
I guess something has changed when it comes to generating the Project graph:
So obviously something is wrong with the project graph. Think I need som help here @Coly010 ! 🙏
I can confirm this issue is happening with Nx version 18.0.4 with a module federation project setup.
Buenas, he tenido el mismo problema, la solución ha sido actualizar las dependencias del proyecto a la última en su momento
El hecho es que tengo una librería desarrollada en V17 y me estaba dando este error.
Actualización [27/04/2024]
La actualización no funcionó
SOLUCIONADO Coloqué en el proyecto que consumirá la librería la siguiente propiedad en el angular.json y funcionó
“preserveSymlinks”: true
Same issue on 18.0.0
I even tried to use the new
loadRemoteModulefrom@nx/angular/mffollowing this guide: https://nx.dev/recipes/angular/dynamic-module-federation-with-angularBut I still get the same error:
I’ve added that repro here: https://github.com/JonWallsten/nx-repro/tree/dynamic