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: 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:

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 14
  • Comments: 36 (7 by maintainers)

Commits related to this issue

Most upvoted comments

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?

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/*" ] }

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:

{
  ...,
  "compilerOptions": {
    ...,
    "paths": {
      "@angular/*": ["./node_modules/@angular/*"],
      "mylib/*": ["../../location/of/my/lib/src/*"]
    }
  },
  ...
}

I had a related problem with the same error message inject() must be called from an injection context. It was solved after setting preserveSymlinks to true as @wvdhaute suggested. I also had warnings that were resolved after setting it:

WARNING in <redacted>/node_modules/@angular/core/fesm5/core.js 15070:15-36
Critical dependency: the request of a dependency is an expression
WARNING in <redacted>/node_modules/@angular/core/fesm5/core.js 15082:15-102
Critical dependency: the request of a dependency is an expression

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 stackblitz

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/*" ] }

This 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
image

 "dependencies": {
    "@angular/animations": "~8.2.5",
    "@angular/common": "~8.2.5",
    "@angular/compiler": "~8.2.5",
    "@angular/core": "~8.2.5",
    "@angular/forms": "~8.2.5",
    "@angular/platform-browser": "~8.2.5",
    "@angular/platform-browser-dynamic": "~8.2.5",
    "@angular/router": "~8.2.5",
    "@datorama/akita": "^4.10.8",
    "rxjs": "^6.5.3",
    "tslib": "^1.10.0",
    "zone.js": "~0.10.2"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.803.9",
    "@angular-devkit/build-ng-packagr": "~0.803.9",
    "@angular/cli": "~8.3.4",
    "@angular/compiler-cli": "~8.2.5",
    "@angular/language-service": "~8.2.5",

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: image I just tried to inject a service from the library! How can i fix it? is it something related to new angular ivy?

update:

  1. 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.

  2. using npm install instead of npm link lead to error too.

  3. 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