question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Angular Upgrade 11 -> 12 Error: Error: inject() must be called from an injection context at injectInjectorOnly

See original GitHub issue

In 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:closed
  • Created 2 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
JoostKcommented, Sep 17, 2021

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.

0reactions
angular-automatic-lock-bot[bot]commented, Oct 18, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular upgrade - Error: inject() must be called from an ...
In this case the injection context is only available in the "primary" runtime, but the parts of the app that were bundled from...
Read more >
NG0203: `inject()` must be called from an injection context ...
You see this error when you try to use the inject() function outside of the allowed injection context. The injection context is available...
Read more >
Error: "inject() must be called from an injection context ...
Description. I am linking a generated web-client into my Angular 11 Web-Application: package.json. "meditation-rest- ...
Read more >
How do I fix "Inject() must be called from an ...
I'm upgrading my Angular books for Angular 13. Angular 13 uses ivy all the ... 2Error: inject() must be called from an injection...
Read more >
Error: inject() must be called from an injection context
I'm having the same issue with Angular 11 and the Diagram component. I've followed the getting started guide to no avail.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found