Error: “Encountered undefined provider! Usually this means you have a circular dependencies”

Error: “Encountered undefined provider! Usually this means you have a circular dependencies”



Here's a somewhat useless error I'm getting in my Angular / TypeScript application. Until someone makes the error message better, what can we do about this? What are the most likely situations that cause this to happen?


Uncaught Error: Encountered undefined provider! Usually this means you have a circular dependencies (might be caused by using 'barrel' index.ts files.
at Object.syntaxError
at eval at Array.forEach (native) [<root>]
at CompileMetadataResolver._getProvidersMetadata
at CompileMetadataResolver.getNgModuleMetadata
at CompileMetadataResolver.getNgModuleSummary
at eval
...






I wish the error would mention the provider in question! In a non-trivial app, there are a lot of providers to narrow down.

– Stan Kurdziel
Jul 9 '18 at 22:30






I don't think it could provide the exact name, since the very problem it's complaining about is it's just getting undefined somewhere in its providers, e.g.: [Foo1, Foo2, undefined, Foo3]. But it could give the index of the entry that's undefined or line number or something. I agree.

– Alexander Taylor
Jul 11 '18 at 1:27


undefined




9 Answers
9



One possibility is trying to declare a service and module in the same file, and declaring the module before the service:


import Injectable, NgModule from '@angular/core';

@NgModule(providers: [FooService]) // WRONG: used before declared
export class FooModule


@Injectable()
export class FooService



You can fix this by declaring the service first, or you can use forwardRef like this:


forwardRef


import forwardRef, Injectable, NgModule from '@angular/core';

@NgModule(providers: [forwardRef(() => FooService)])
export class FooModule


@Injectable()
export class FooService






Works perfectly! Is there a scenario where creating a forwardRef like this would be a bad idea?

– cjsimon
Apr 27 '17 at 2:32






I don't think forwardRef is ever dangerous, just unnecessary if your symbols are declared before theyre used. Sometimes it's better to put things in separate files though, but small/trivial module declarations aren't always worth the extra file.

– Alexander Taylor
Apr 27 '17 at 6:50






I have my components in separate files. Would it be enough to declare the provider module first, or does it have to be initialized on the page before the other module?

– cjsimon
Apr 27 '17 at 21:07






If you're using TypeScript, and you have a file that imports another file, I think the compiler will cause everything to be loaded in the correct order. Sorry I just saw your comment; if you got this error any my solution didn't fix it, please post another solution :)

– Alexander Taylor
May 3 '17 at 16:38






Your solution works for me. I was just wondering if the provider module had to be initialized before use, though it seems that simply declaring it is enough to get it working, as you have shown in your answer

– cjsimon
May 4 '17 at 21:35



It is very hard to tell from the error message which provider causes this issue. The way I managed to debug this is the following:






while "cave man like" (surely the tool could output something if we ourselves can make this simple edit to get some context), this suggestion does at least let us get some info re the culprit.

– Neil Clayton
Jul 30 '18 at 20:18






I added a console.log(looking for $provider, $debugInfo); The provider might be undefined, but the debugInfo gives on the context of the failure.

– Neil Clayton
Jul 30 '18 at 21:20



looking for $provider, $debugInfo






I had this error, and it turned out it was a problem with a barrel file. Once I stopped importing the barrel file and using it's exports and started to import those things directly, it worked. Took me two days though. Upgrading from 5 => 6 is quite a bit of work.

– frosty
Sep 6 '18 at 19:30






I used the above code from Neil console.log(`looking for $provider, $debugInfo`); and added the --verbose flag to my ng build --prod command and found it fairly helpful.

– Jeph
Sep 14 '18 at 17:48



console.log(`looking for $provider, $debugInfo`);


ng build --prod






Instead of manipulating the angular bundle files, one may also put a breakpoint in the corresponding line in compiler.js file that includes the error message using browser dev tools. Once the breakpoint is hit, I investigated the call stack and found the providers array, which based on the types of the other imports helped me to trace down the faulty provider.

– Capricorn
Nov 1 '18 at 23:14


compiler.js


providers



Check if the module can find the service you have mentioned.



In my case I was exporting a guard from my guards folder. this folder contained an index.ts file. There were two more files in this guards folder auth.guard.ts and company.guard.ts.
Ideally I should have exported these files in the index as follows:


guard


guards


guards


auth.guard.ts


company.guard.ts



contents of guards/index.ts


export * from './auth.guard';
export * from './company.guard'; // forgot this



But I forgot to include the line above that exports from company.guard.ts. This was creating problem.


company.guard.ts



Got this error running --prod.



You can't import things like that:


import MyService from '.';



You should use the full path


import MyService from './my.service'



I got this error when missing an import for an override of an angular class. I imagine an incorrect import may cause the error in other ways also.



In my case I had no import statement for File and it defaulted to a File interface which wasn't what I wanted. Adding import File from "@ionic-native/file" fixed the problem.


File


import File from "@ionic-native/file"



I also console logged the value right before the error message statement in node_modules@angularcompilerbundlescompiler.umd.js file.



And checked that Document interface was there in providers array of a component which was the root cause.



I removed it to fix this issue.



in my case i changes this


@Injectable()
export class LocationTracker



to


@Injectable()
export class LocationTrackerProvider



I was running into this while using ng-packagr to package a library then importing it into another library. What ended up being my problem was indeed the 'barrel' index.ts imports.



This was making it break


import Activate, Another, Data from './services
@NgModule( providers: [ Activate, Another, Data ])



where in the services folder I had one index.ts file that was exporting all of the services.



This fixed it:


import Activate from './services/activate.service.ts'
import Another from './services/another.service.ts'
import Data from './services/data.service.ts'
@NgModule( providers: [ Activate, Another, Data ])



Sometime this issue occurred because of some dependency in third party api used in angular app. I faced same issue and resolved it using following steps:



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ