Guy Park
Guy Park

Reputation: 1027

Failed to instantiate module in AngularJS due to missing (loaded) provider

Working on an old project using AngularJS, and seem to be getting the good old "Failed to instantiate module" error due to "Unknown provider", but the funny thing is that the module that provides this "missing" provider has been loaded, and the Provider is part of the module. (I know this since the main module also uses the same Module/Provider)

(Dependancy Tree)

  Main Module
    - API Module
    - SubModule   <-- Fails when this is added
       - API Module

The Main Module loads uses a [custom-built] API Module, the sub-module I'm loading into the Main Module also uses the API Module, but when I add the SubModule it all fails, even when the Provider (from API Module) works perfectly.

Again the Main Module is using the Provider from the API, so another reason I know that the Provider is loaded in.

I've resorted the dependancy order in both Modules, and have checked the order in which the Modules load, and it all seems in order.

Any ideas of what might be throwing this error?

Upvotes: 0

Views: 55

Answers (1)

Guy Park
Guy Park

Reputation: 1027

Figured out what it was. This all came about from refactoring the existing app code, which worked. What had happened was that my Provider was actually loading a Factory. What it used to be was

mymodule.factory('substuff', function() {....});

What the refactoring was changed too, was...

import SubStuffProvider from 'app/providers/SubStuff.provider'

....
mymodule.provider('substuff', SubStuffProvider);

However, the item I was importing was exporting a Factory (even though the file was labelled substuff.provider.js) and not a Provider that I thought, so it seems that it was loading a Provider with a Factory, which I believe is in the wrong load order for AngularJS.

Anyway, fixing my imported file to load the "Provider" file as the factory, as it should have done, and it works.

Can't wait to port this thing over to Angular 6.

Upvotes: 0

Related Questions