angular: Error: inject() must be called from an injection context
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:
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:
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 14
- Comments: 36 (7 by maintainers)
Links to this issue
Commits related to this issue
- Merge pull request #14 from hapinessjs/next release(version): v6.4.2 to fix angular/angular#26128 and angular/angular#25813 — committed to hapinessjs/ng-elements-loader by akanass 6 years ago
- Tweaked compiler settings. See: https://github.com/angular/angular/issues/25813 I'm receiving this runtime error in a testing context: Error: inject() must be called from an injection context The ... — committed to D4H/api-angular by bhalash 5 years ago
- Fix https://github.com/angular/angular/issues/25813#issuecomment-584866010 — committed to codeboss-co/codeboss-angular-libs by dillancagnetta 3 years ago
in angular.json of the project calling the angular lib via the link
projects.$name.architect.build.options.preserveSymlinks: true
note: make sure to set preserveSymlinks to true, fixed our issue
Finally i solved it, I put this path in the compilerOptions section of tsconfig.app.json of the angular application:
"paths": { "@angular/*": [ "../node_modules/@angular/*" ] }
The problem seems to occur the moment we use
yarn link
to test the library in an application. Does Angular treat linked packages differently and somehow enables Ivy then?I got this issue in Angular 8, I am using Ionic 4. Can someone help please to sort this issue?
For me,
"@angular/*": [ "./node_modules/@angular/*" ]
(one dot, not two in the path) was what I needed, in addition to my own other mappings for my libraries. This only applies when using TypeScript path mapping instead of linking to the libraries, as documented here. Also, I put it in the tsconfig.json, not tsconfig.app.json. Either worked.So the paths portion of my tsconfig.json file in my main Angular app is:
I had a related problem with the same error message
inject() must be called from an injection context
. It was solved after settingpreserveSymlinks
totrue
as @wvdhaute suggested. I also had warnings that were resolved after setting it:I am also working with a service from a library that is npm linked.
I am on Angular 7.
Because the error only seems to occur when the service injecting
NgModuleRef
is in a library, at first glance Stackblitz didn’t seem to support 2 projects (both library and app) in one project. if I’m mistaken, I’m happy to make a reproduction of both on stackblitzThis solved the issue for me. However I am not using npm or yarn link, I am using the tsconfig paths to link directly to a library that sits on a different project.
@shabbir7252
my info here sir
I have the same problem, my workspace includes 2 angular applications (version 8.0) and an angular library. I installed the library in the applications by ‘npm link’ command. I also added preserveSymlinks: true to the angular.json of both applications. Now, one application works perfect and the other doesn’t and below error is shown: I just tried to inject a service from the library! How can i fix it? is it something related to new angular ivy?
update:
I found if i inject a simple service from library then application works perfect, but if i inject a service which another service injected to it, then above error emerges.
using
npm install
instead ofnpm link
lead to error too.both applications updated from angular v7 to angular v8.
@alxhub unfortunately they’re propriety libraries developed in house. I did provide a repository here that demonstrates the functionality