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.

Error: inject() must be called from an injection context

See original GitHub issue

I’m submitting a…


[X] Regression (a behavior that used to work and stopped working in a new release)
[ ] Bug report  
[ ] Performance issue
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question
[ ] Other... Please describe:

Current behavior

While creating a library I noticed that if you consume the library and try to inject a service that in turn has a dependency on either NgModuleRef, Injector or ApplicationModule it throws an error as shown below: 2018-09-05 14_52_21-

Expected behavior

In Angular 5 we injected NgModuleRef without any problems, in Angular 6 one can still inject these classes, just not from anything that’s in a library.

in other words, in Angular 5 this code worked fine, but breaks in Angular 6:

public constructor(private readonly appModule: NgModuleRef<any>) {
	//
}

Minimal reproduction of the problem with instructions

I’ve created a Github repository showcasing the issue here

What is the motivation / use case for changing the behavior?

We inject NgModuleRef in our company’s internal libraries for on the fly reconfiguration of services, for this we need an injector that can resolve those services.

If there’s a better way of accessing the injector from a service (in a library, from application itself it works!), that’s a solution for me too.

Environment


Angular version: 6.1.6


Browser:
- [X] Chrome (desktop) version 68.0.3440.106
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [X] Firefox version 61.0.2
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [X] Edge version 42.17134.1.0
 
For Tooling issues:
- Node version: 9.11.1
- Platform:  Windows 10

Others:

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:14
  • Comments:36 (5 by maintainers)

github_iconTop GitHub Comments

32reactions
wvdhautecommented, Nov 20, 2018

in angular.json of the project calling the angular lib via the link

projects.$name.architect.build.options.preserveSymlinks: true

30reactions
wvdhautecommented, Nov 20, 2018

note: make sure to set preserveSymlinks to true, fixed our issue

Read more comments on GitHub >

github_iconTop Results From Across the Web

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 >
inject() must be called from an injection context - Stack Overflow
There seems to be an issue when using npm link when consuming the library. Solution: use projects.$name.architect.build.options.
Read more >
Error: "inject() must be called from an injection context" after ...
After running ng serve the app crashes. See error message below. Exception or Error. Error: inject() must be called from an injection context...
Read more >
Error: inject() must be called from an injection context | Angular
Error : inject() must be called from an injection context. Hello, I am trying to use AngularJS Grid, but getting the error and...
Read more >
How do I fix "Inject() must be called from an injection context"?
I had to enable "preserveSymLinks" in the Angular. json of the main project. 18 }, Once that was in, I was able to...
Read more >

github_iconTop Related Medium Post

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