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.

APP_INITIALIZER ->Cannot instantiate cyclic dependency!

See original GitHub issue

🐞 bug report

Description

It is just present on new Angular 9. Unable to use APP_INITIALIZER with Router anymore. Angular 8 works like a charm!

Here app.module code:


@NgModule({
  imports:      [ BrowserModule, FormsModule, AppRoutingModule],
  declarations: [ AppComponent, HelloComponent ],
  providers: [
    {provide: APP_BASE_HREF, useValue : '/' },
    {
      provide: APP_INITIALIZER,
      useFactory: () => () => { },
      multi: true,
      deps: [TitleService]
    }
  ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

🔬 Minimal Reproduction

Just clone repository: https://github.com/meriturva/angular-9-cyclic-dependency-error and run ng serve

🔥 Exception or Error


Error: Cannot instantiate cyclic dependency! ApplicationRef
    at throwCyclicDependencyError (core.js:5469)
    at R3Injector.push../node_modules/@angular/core/__ivy_ngcc__/fesm5/core.js.R3Injector.hydrate (core.js:11343)
    at R3Injector.push../node_modules/@angular/core/__ivy_ngcc__/fesm5/core.js.R3Injector.get (core.js:11170)
    at injectInjectorOnly (core.js:787)
    at ɵɵinject (core.js:792)
    at injectArgs (core.js:887)
    at Object.factory (core.js:11436)
    at R3Injector.push../node_modules/@angular/core/__ivy_ngcc__/fesm5/core.js.R3Injector.hydrate (core.js:11347)
    at R3Injector.push../node_modules/@angular/core/__ivy_ngcc__/fesm5/core.js.R3Injector.get (core.js:11170)
    at injectInjectorOnly (core.js:787)

🌍 Your Environment

Angular Version:



     _                      _                 ____ _     ___ 
    / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
   / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | | 
  / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
 /_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
                |___/
    

Angular CLI: 9.0.1
Node: 12.15.0
OS: win32 x64

Angular: 9.0.0
... common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router
Ivy Workspace: Yes

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.900.1
@angular-devkit/build-angular     0.900.1
@angular-devkit/build-optimizer   0.900.1
@angular-devkit/build-webpack     0.900.1
@angular-devkit/core              9.0.1
@angular-devkit/schematics        9.0.1
@angular/cli                      9.0.1
@ngtools/webpack                  9.0.1
@schematics/angular               9.0.1
@schematics/update                0.900.1
rxjs                              6.5.4
typescript                        3.7.5
webpack                           4.41.2

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:8
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
AndrewKushnircommented, Feb 24, 2020

Hi @meriturva,

Thanks for reporting the problem and providing a repro.

We performed the investigation and found out that Ivy throws an exception correctly (while View Engine was not doing that as expected, likely due to a bug), since there is a dependency cycle in the code as shown below:

AppModule (root module) -> ApplicationRef -> ApplicationInitStatus ->
APP_INITIALIZER -> TitleService -> Router -> ApplicationRef (that creates a cycle)

It turned out that the ApplicationRef dependency in Router is not required, so it was removed in #35642. I tested your repro case with the changes from #35642 and it seems to be working correctly now. The changes from #35642 should be available in 9.0.3 release of the framework in a few days.

Thank you.

1reaction
julmotcommented, Feb 7, 2020

I’m also experiencing an issue with a cyclic dependency in Angular v9 that didn’t pop up in Angular v8. The background is that I have two services:

  • Service A: Loads service B
  • Service B: Loads only interfaces from the file of service A

So there’s no cyclic dependency on a service layer, since service B isn’t loading service A, it’s just loading interfaces from the same file in which service A is located.

Of course refactoring this to a central interface file would solve the issue, but I don’t see why this is an issue in Angular v9 if it wasn’t an issue in v8?

The issue is logged to the console as warning when building the application.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular 8: Cannot instantiate cyclic dependency
I am trying to integrate the APP_INITIALIZER from Angular in my project in order to do some functionalities before start the application. The ......
Read more >
Cannot instantiate cyclic dependency! ApplicationRef ("[ERROR
I'm submitting a... [ ] bug report [ ] feature request [x] support request Actual behavior Installed the module like described here in...
Read more >
[6.1.2] Toast Module: Cannot instantiate cyclic dependency
Hello, I'm trying to use the Pro Angular Toast/Alert service and I keep getting this error: Uncaught Error: Provider parse errors: Cannot instantiate...
Read more >
Angular – APP_INITIALIZER raises “Cannot instantiate cyclic ...
Angular – APP_INITIALIZER raises “Cannot instantiate cyclic dependency! ApplicationRef_” when used with a custom Http provider that is redirecting.
Read more >
Circular Dependency In Di Detected For Applicationref. How ...
I am trying to load the data using APPINITIALIZER but getting below error Uncaught Error: Provider parse errors: Cannot instantiate cyclic dependency!
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