AngularJS: Uncaught Error: [$injector:modulerr] Failed to instantiate module?
Asked Answered
C

7

21

I am new to AngularJS and working my way through some documents and tutorials to learn. My question is in reference to Egghead's video series, this video in particular, demonstrating how to put together a basic search filter. I wanted to use this in a real app I'm building for a friend with a small candle-making business but when I modified it to be her candles rather than the Avengers cast (as demo'd in the video) I got this error:

Uncaught Error: [$injector:modulerr] Failed to instantiate module myApp due to:

Error: [$injector:nomod] Module 'myApp' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify ...

I copied a redacted (only 3 cast members in the array) version of EXACTLY what is in the video demo into a jsfiddle and discovered it still yields the same error. (For reference, the Egghead demo is here: http://www.thinkster.io/angularjs/ET1iee6rnm/angularjs-ngfilter). I've read at least a half dozen similar questions on this site so far and tried every solution offered, but none of them get rid of this error or cause the Avengers search -- which works fine in the video demo -- to actually function properly.

HTML:

<div ng-app="myApp">
    <div ng-controller="AvengersCtrl">
        <input type="text" ng-model="search.$" />
        <table>
            <tr ng-repeat="actor in avengers.cast | filter:search">
                <td>{{actor.name}}</td>
                <td>{{actor.character}}</td>
            </tr>
        </table>
    </div>
</div>

Javascript:

var myApp = angular.module('myApp', []);
myApp.factory('Avengers', function () {
    var Avengers = {};
    Avengers.cast = [
        {
        name: "Robert Downey Jr.",
        character: "Tony Stark / Iron Man"
        }, 
        {
        name: "Chris Evans",
        character: "Steve Rogers / Captain America"
        },
        {
        name: "Mark Buffalo",
        character: "Bruce Banner / The Hulk"
        }
    ];
    return Avengers;
})

function AvengersCtrl($scope, Avengers) {
    $scope.avengers = Avengers;
}

Simply put, can someone offer a solution that will work and get rid of this error, as well as explain in simple English (not Ph.D. level "Angular Obscurese") what causes it (in a nutshell) and what needs to be done to avoid it?

Edit: Apologies, but the jsfiddle link referenced above from the tutorial is no longer active. I have removed the broken link. The tutorial mentioned is still available for viewing.

Cree answered 14/3, 2014 at 13:39 Comment(10)
Try using No Wrap - In Head in your fiddle: jsfiddle.net/Q5hd6Selfhypnosis
Do not load it in head, but at the end of the body !Snowstorm
@ExpertSystem: body or head both work in this case. I also added an explanation in my answer.Selfhypnosis
@ExpertSystem: one more thing, that loading option applies to our script in the script window, not the framework script.Selfhypnosis
@KhanhTO: I know both work, but the one is bad practice (loading it in head) the other is good practice (loading it in body). So, between two working solutions, you should not encourage the worse.Snowstorm
@ExpertSystem: That's true as it will appear that the page loads faster. But in case of angular, it's more likely to display uncompiled DOM. Something like {{}}Selfhypnosis
@KhanhTO: And that's exactly what the ng-cloak can be used for :)Snowstorm
@ExpertSystem: thanks, I did not know ng-cloak.Selfhypnosis
let us continue this discussion in chatSnowstorm
Lol'd @Mark "Buffalo"Consultation
S
52

Try using No Wrap - In Head or No wrap - in body in your fiddle:

Working fiddle: http://jsfiddle.net/Q5hd6/

Explanation:

Angular begins compiling the DOM when the DOM is fully loaded. You register your code to run onLoad (onload option in fiddle) => it's too late to register your myApp module because angular begins compiling the DOM and angular sees that there is no module named myApp and throws an exception.

By using No Wrap - In Head, your code looks like this:

<head>

    <script type='text/javascript' src='//cdnjs.cloudflare.com/ajax/libs/angular.js/1.2.1/angular.js'></script>

    <script type='text/javascript'>
      //Your script.
    </script>

</head>

Your script has a chance to run before angular begins compiling the DOM and myApp module is already created when angular starts compiling the DOM.

Selfhypnosis answered 14/3, 2014 at 13:55 Comment(6)
Excellent, thanks for your response. However, I am still getting an error when I attempt to develop this locally and fire it up in my browser. All that shows up is the input field, no table. Specifically I am getting two errors: -Uncaught ReferenceError: angular is not defined kandlepicker.js:1 and -Error: [$injector:unpr] errors.angularjs.org/1.2.14/$injector/…. What am I doing wrong? I have the script loading reference to angular just before the closing </body> tag as recommended and my custom script in the <head> section. Tried other ways, no goCree
When I put both script tags together (Angular first, custom second) in either <head> OR at the end of <body>, I get this error still: -- Error: [$injector:unpr] errors.angularjs.org/1.2.14/$injector/… ----> I'm guessing from this: docs.angularjs.org/error/$injector/unpr -- that I have to specify a "Service". But what if I don't actually need one? Sorry if this sounds dumb, like I said I'm new to Angular. Maybe my questions will help other newbies. :)Cree
@code-sushi: firstly, angular script must always be loaded before your custom script or an exception angular is not defined will be thrown.Selfhypnosis
@code-sushi: your kandlepicker.js should be after angular. Also check if you inject a service to the controller without defining it. If you don't need that service in your controller, just remove it.Selfhypnosis
Hi, this works in fiddle, but how do you fix the problem in an html browser?Cleome
@Dirk: as in the code sample, you could run your script in the head right after including angular.jsSelfhypnosis
G
10

You have to play with JSFiddle loading option :

set it to "No wrap - in body" instead of "onload"

Working fiddle : http://jsfiddle.net/zQv9n/1/

Gigot answered 14/3, 2014 at 13:45 Comment(0)
W
3

For people having the same error with a similar code:

$(function(){
    var app = angular.module("myApp", []); 
    app.controller('myController', function(){

    });
});

Removing the $(function(){ ... }); solved the error.

Windowlight answered 11/10, 2016 at 9:36 Comment(0)
G
1

For people who find this old posting on the web by searching for the error message, there is another possible cause of the problem.

You could just have a typo in your call to the script, even if you have already done the things described in the other excellent answer. So check to make sure you can used the right spelling in your script tags.

Godrich answered 26/10, 2015 at 20:38 Comment(0)
P
1

it turns out that I got this error because my requested module is not bundled in the minification prosses due to path misspelling

so make sure that your module exists in minified js file (do search for a word within it to be sure)

Presumptuous answered 3/10, 2017 at 15:26 Comment(0)
V
0

I had to change the js file, so to include "function()" at the beginning of it, and also "()" at the end line. That solved the problem

Variegated answered 5/11, 2015 at 14:3 Comment(3)
Was this in jsfiddle? The question is specifically pertaining to that, so if it is, can you please post a link to your fiddle? Thanks!Cree
This works in jsFiddle without adding the modification I explained, I tried your code in jsfiddle and it works properly, see the code here jsfiddle.net/SaifHarbia/ht4jme7q note that you need to choose an Angular library and No Wrap- in <body> under Frameworks and ExtensionsVariegated
Well yes, you have simply repeated the first answer offered over a year and a half ago by Khanh TO, above. That was the issue -- use no-wrap.Cree
H
0

I know its know a 9 year old question, I was having this same error, I tried to put a js file containing AngularJS code in a directory where I also had the following files

enter image description here

I tried several things with no success until I just made a simple test to move outside that directory, isolating the file and that worked, I still don't know the reason. Hope this helps to someone

Henson answered 14/2, 2023 at 17:43 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.