Angular Upgrade 11 -> 12 Error: Error: inject() must be called from an injection context at injectInjectorOnly
See original GitHub issueIn line with the protocol, I have tried asking on StackOverflow first, but am not getting any answers.
I have an angular site:
Site1
- Module1
- Module2
- SharedModule <- NOT in a library, just a module
Site2
- Module3
- Module4
- SharedModule <- calls module in Site1 directly
It was created in Angular 8 and has upgraded just fine so far. But going 11 -> 12, I’m having trouble.
Site1 runs just fine. Site 2 builds, but at runtime errors with the following:
Error: inject() must be called from an injection context at injectInjectorOnly (core:4745) at Module.ɵɵinject (core:4755) at Object.RouterModule_Factory [as factory] (router:5999) at R3Injector.hydrate (core.js:11438) at R3Injector.get (core.js:11257) at core.js:11295 at Set.forEach (<anonymous>) at R3Injector._resolveInjectorDefTypes (core.js:11295) at new NgModuleRef$1 (core.js:25325) at NgModuleFactory$1.create (core.js:25379)
I’ve researched this extensively, but can’t fix it. The error message is meaningless, so I’m just guessing the issue is that shared module.
Reading another github issue, I’ve tried adding "preserveSymlinks": true,
to projects.$name.architect.build.options, but that doesn’t work. I’m not using npm link
as the other module isn’t a library (I know it should have been set up that way, but it isn’t).
I’m facing a lot of work to re-architect the site, so would appreciate any help getting the upgrade to work.
Do you know how to fix this please?
If this can’t be fixed, could Angular 13 at least give a more helpful error message / a build time failure?
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (3 by maintainers)
Top GitHub Comments
The cause is that the two separate workspaces both have their own
@angular/core
packages, and both end up in the bundled application. The Angular runtime keeps its state in top-level module variables that are private to that module, but having two occurrences of that module (from both instances of@angular/core
) you run into situations where state is only initialized correctly in one instance, not the other. In this case the injection context is only available in the “primary” runtime, but the parts of the app that were bundled from the “secondary” library parts will depend on the “secondary” runtime state.This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.