"inject() must be called from an injection context" when importing angular app using module federation architecture
Asked Answered
D

3

5

I am trying to implement the micro-frontend concept in angular using webpack5 module federation by following this example Module federation with angular, but i am facing this error Error: Uncaught (in promise): Error: inject() must be called from an injection context

I tried the solutions provided in this answer inject() must be called from an injection context but it didn't work.

Currently i am using angular version 11 in both the applications.

package.json of the app being imported (child)

    "resolutions": {
    "webpack": "5.0.0"
  },
  "dependencies": {
    "@angular/animations": "11.0.0-next.6",
    "@angular/cdk": "11.0.0-next.6",
    "@angular/common": "11.0.0-next.6",
    "@angular/compiler": "11.0.0-next.6",
    "@angular/core": "11.0.0-next.6",
    "@angular/forms": "11.0.0-next.6",
    "@angular/platform-browser": "11.0.0-next.6",
    "@angular/platform-browser-dynamic": "11.0.0-next.6",
    "@angular/platform-server": "^11.0.0",
    "@angular/router": "11.0.0-next.6",
    "@angular/service-worker": "11.0.0-next.6",
    "@fortawesome/angular-fontawesome": "^0.8.0",
    "@fortawesome/fontawesome-svg-core": "^1.2.32",
    "@fortawesome/free-solid-svg-icons": "^5.15.1",
    "@ngx-translate/core": "^13.0.0",
    "@ngx-translate/http-loader": "^6.0.0",
    "angular-font-awesome": "^3.1.2",
    "bootstrap": "^4.3.1",
    "classlist.js": "^1.1.20150312",
    "core-js": "^3.1.4",
    "file-saver": "^2.0.2",
    "font-awesome": "^4.7.0",
    "js2xmlparser": "^4.0.0",
    "moment": "^2.25.3",
    "moment-timezone": "^0.5.28",
    "ngx-webstorage-service": "^4.1.0",
    "node-sass": "^4.12.0",
    "post-robot": "7.0.15",
    "primeicons": "^1.0.0",
    "rxjs": "6.6.3",
    "rxjs-tslint": "^0.1.7",
    "zone.js": "^0.10.3",
    "@angular-architects/module-federation": "^0.9.3"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "0.1100.0-next.6",
    "@angular/cli": "~11.0.1",
    "@angular/compiler-cli": "11.0.0-next.6",
    "@angular/language-service": "^11.0.0",
    "@types/jasmine": "~3.6.0",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~12.6.8",
    "codelyzer": "^6.0.0",
    "jasmine-core": "~3.6.0",
    "jasmine-spec-reporter": "~5.0.0",
    "karma": "~5.0.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage-istanbul-reporter": "~3.0.2",
    "karma-jasmine": "~4.0.0",
    "karma-jasmine-html-reporter": "^1.5.0",
    "protractor": "~7.0.0",
    "ts-node": "~8.3.0",
    "tslint": "~6.1.0",
    "typescript": "~4.0.5",
    "serve": "^11.3.2",
    "ngx-build-plus": "^10.1.1"
  }

package.json of the app importing another app (parent)

"resolutions": {
    "webpack": "5.0.0"
  },
  "dependencies": {
    "@angular-architects/module-federation": "^0.9.3",
    "@angular/animations": "^11.0.0",
    "@angular/cdk": "^11.0.0",
    "@angular/common": "11.0.0-next.6",
    "@angular/compiler": "11.0.0-next.6",
    "@angular/core": "11.0.0-next.6",
    "@angular/forms": "11.0.0-next.6",
    "@angular/platform-browser": "11.0.0-next.6",
    "@angular/platform-browser-dynamic": "11.0.0-next.6",
    "@angular/router": "11.0.0-next.6",
    "@ngx-translate/core": "^12.1.2",
    "@ngx-translate/http-loader": "^5.0.0",
    "bootstrap": "^4.5.2",
    "bowser": "^2.11.0",
    "classlist": "^2.0.0",
    "classlist.js": "^1.1.20150312",
    "core-js": "^3.6.5",
    "dc-central-modules": "^0.1.27",
    "moment-timezone": "^0.5.28",
    "post-robot": "7.0.15",
    "primeng": "^9.0.0-rc.1",
    "query-string": "^6.13.7",
    "rxjs": "~6.5.4",
    "tslib": "^2.0.0",
    "zone.js": "~0.10.2"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "0.1100.0-next.6",
    "@angular/cli": "~11.0.1",
    "@angular/compiler-cli": "11.0.0-next.6",
    "@types/jasmine": "~3.6.0",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "^12.11.1",
    "codelyzer": "^6.0.0",
    "jasmine-core": "~3.6.0",
    "jasmine-spec-reporter": "~5.0.0",
    "karma": "~5.0.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage-istanbul-reporter": "~3.0.2",
    "karma-jasmine": "~4.0.0",
    "karma-jasmine-html-reporter": "^1.5.0",
    "karma-phantomjs-launcher": "^1.0.4",
    "protractor": "~7.0.0",
    "serve": "^11.3.2",
    "ts-node": "~8.3.0",
    "tslint": "~6.1.0",
    "typescript": "~4.0.5",
    "ngx-build-plus": "^10.1.1"
  }
Drier answered 25/11, 2020 at 12:38 Comment(2)
Did you find a solution?Idalla
@Idalla I added solution below which solved mine issue, you can check and verify if works.Millett
M
1

In my case, I was using requiredVersion: 'auto' and sharedMappings.getDescriptors() from @angular-architects/module-federation package. so instead I specified exact version.

"@angular/core": { singleton: false, strictVersion: true, requiredVersion: '12.2.15' },
"@angular/common": { singleton: false, strictVersion: true, requiredVersion: '12.2.15' },
"@angular/router": { singleton: false, strictVersion: true, requiredVersion: '12.2.15' },
Millett answered 17/1, 2022 at 23:37 Comment(0)
S
5

Faced same issues, the error was solved after figuring out @angular/core and @angular/common (and possibly @ngx-translate/core) must be shared and loaded once with "singleton: true" flag in both your webpack configurations.

Supen answered 20/1, 2021 at 13:59 Comment(1)
@SabitostandswithUkraine In which file we have to add this part?Unveil
M
1

In my case, I was using requiredVersion: 'auto' and sharedMappings.getDescriptors() from @angular-architects/module-federation package. so instead I specified exact version.

"@angular/core": { singleton: false, strictVersion: true, requiredVersion: '12.2.15' },
"@angular/common": { singleton: false, strictVersion: true, requiredVersion: '12.2.15' },
"@angular/router": { singleton: false, strictVersion: true, requiredVersion: '12.2.15' },
Millett answered 17/1, 2022 at 23:37 Comment(0)
C
0

This possible occur when any of your child dependency not met with angular core or angular common. Try checking consoles/terminal for more information. In my case, for eg: highchart I have installed in child component and getting peer dependency on angular core 10.0.0 with parent. Error was gone once I had updated the version.

Cide answered 21/1, 2021 at 5:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.