AngularJS 1.2 $injector:modulerr
Asked Answered
C

18

450

When using angular 1.2 instead of 1.07 the following piece of code is not valid anymore, why?

'use strict';

var app = angular.module('myapp', []);

app.config(['$routeProvider', '$locationProvider',
    function($routeProvider, $locationProvider) {
        $locationProvider.html5Mode(true);
        $routeProvider.
        when('/', {
            templateUrl: 'part.html',
            controller: 'MyCtrl'
        }).
        otherwise({
            redirectTo: '/'
        });
    }
]);

the issue is in the injector configuration part (app.config):

Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.2.0rc1/$injector/modulerr?p0=muninn&p1=Error%…eapis.com%2Fajax%2Flibs%2Fangularjs%2F1.2.0rc1%2Fangular.min.js%3A31%3A252) 

If I remember correctly this issue started with angular 1.1.6.

Calderon answered 17/8, 2013 at 9:56 Comment(0)
C
636

The problem was caused by missing inclusion of ngRoute module. Since version 1.1.6 it's a separate part:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.0rc1/angular-route.min.js"></script>

var app = angular.module('myapp', ['ngRoute']);
Calderon answered 17/8, 2013 at 10:10 Comment(16)
If you are not sure which module is missing, use the not minified angular.js which gives a readable error message:"Error: [$injector:nomod] Module 'ngRoute' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument."Twocycle
Hmm, I'm using bower, and it provides .map files - I wonder why Chrome was still showing me errors from the minified file. Thanks for the heads up, @Mart!Triny
Why would they do this??? is really ngRoute a service that should be taken out of the core angular services? i don't get it.Unsupportable
@arg20, you will find the answer here: github.com/angular/angular.js/commit/…Sebastian
not working for me with ngroute included and working with version 1.2.0.rc-3Virgate
Holy cow I have been looking EVERYWHERE for the solution to this. I upgraded from 1.0 to 1.2 and got this error. The only advice I could find is to make sure angular-route.js was included, which it was.... Not a fan of the Angular docs. Anyway thank you! I up-voted.Amass
You might get this error if you use angular.module('myModule') instead of angular.module('myModule',[]) while defining your module. #16261038Subreption
I'm getting a MINERR_ASSET not found even after including the modules, see my question here please! #21455870Dryer
@Twocycle thanks using non minified Angular shows exact error. I had problem with var App spelling Uppercase.Intercollegiate
Its always a good idea to use the non-minified version of AngularJS while developing. When app is tested and passes the test and is ready to be deployed, use the minified version. Debugging on the non-minified version is much easier.Sterilize
Can anyone tell why I'm still getting that exact error? I implemented the fix and added ngRoute #25705938Platitudinize
@Twocycle thanks for the comment, which helps me to fix my ngResource problem.Afroasian
I had a third party library included twice... got the same error message.Schnozzle
@Twocycle I can't believe why your comment is not an answer?Whensoever
@OmarTariq thanks, but technically this is only helping find the solution, not the actual solution. It is more general that this very question.Twocycle
@Twocycle I can't believe I didn't know that until now. I have been working on Angular now for years and plagued with Angular's non-specific error messages. Thanks for that tip as that will be a life saver.Lat
A
42

my error disappeared by adding this '()' at the end

(function(){
    var home = angular.module('home',[]);

    home.controller('QuestionsController',function(){
        console.log("controller initialized");
        this.addPoll = function(){
            console.log("inside function");
        };
    });
})();
Aimo answered 15/11, 2014 at 19:43 Comment(0)
F
40

One more thing to add to the list as this is the first result that comes up with a google search of 'Error: [$injector:modulerr] angular':

If you have a mismatch between your app name in your 'index'html' and in your main javascript app definition this can also generate this error.

For example if your HTML looks like this:

    </head>
    <body ng-app="myWebSite">

        <!-- My Web Site -->
        <p>About my web site...</p>

        etc ...

And your JavaScript looks like this (i.e has a typo on app name - myWebCite instead of myWebSite):

/** Main AngularJS Web Application */ 
var app = angular.module('myWebCite', [ 'ngRoute' ]); 

/** Configure the Routes */ 
app.config(['$routeProvider', function ($routeProvider) { 
  etc ...

then the 'Error:[$injector:modulerr] angular' error will be generated also.

Fortis answered 27/10, 2014 at 18:38 Comment(1)
I had very close to this. I had set the ng-app="app"... and then never setup a module. So it was tripping the error. As a beginner, I just removed the ="app" piece to get my sample code working. Later, I setup the proper module and routing... :)Tingle
B
26

A noob error can be forgetting to include the module js

<script src="app/modules/myModule.js"></script>

files in the index.html at all

Bast answered 17/8, 2015 at 11:44 Comment(3)
Amazing how bad the debugging/error messages are in angular.Olly
LOL. I said to my self, no, I can't make this simple error. And I passed your answer multiple times. But it solved my problem.Lysenko
It's about some import missingIsolde
T
13

If you have this error in console ([$injector:nomod], MINERR_ASSET:22), make sure you are not including your application code before loading AngularJS

I was doing that and once I fixed the order, the error went away.

Toxicant answered 10/5, 2014 at 3:11 Comment(0)
F
13

For those using frameworks that compress, bundle, and minify files, make sure you define each dependency explicitly as these frameworks tend to rename your variables. That happened to me while using ASP.NET BundleConfig.cs to bundle my app scripts together.

Before

app.config(function($routeProvider) {
    $routeProvider.
      when('/', {
          templateUrl: 'list.html',
          controller: 'ListController'
      }).
      when('/items/:itemId', {
          templateUrl: 'details.html',
          controller: 'DetailsController'
      }).
      otherwise({
          redirectTo: '/'
      });
});

After

app.config(["$routeProvider", function($routeProvider) {
    $routeProvider.
      when('/', {
          templateUrl: 'list.html',
          controller: 'ListController'
      }).
      when('/items/:itemId', {
          templateUrl: 'details.html',
          controller: 'DetailsController'
      }).
      otherwise({
          redirectTo: '/'
      });
}]);

Read more here about Angular Dependency Annotation.

Forthright answered 28/12, 2014 at 12:47 Comment(0)
C
9

I have just experienced the same error, in my case it was caused by the second parameter in angular.module being missing- hopefully this may help someone with the same issue.

angular.module('MyApp');

angular.module('MyApp', []);

Cuyler answered 5/6, 2016 at 17:54 Comment(1)
This was my issue; I was missing a declaration of angular.module('my.Namespace', []); before using angular.module('my.Namespace').directive('myDirective', [...Homograph
O
7
add to link
<script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.0/angular-route.min.js"></script>

var app = angular.module('apps', [ 'ngRoute' ]); 
Overzealous answered 30/3, 2015 at 6:29 Comment(1)
Forgot that you must include src when using specific dependencies!Olly
E
6

I had the same problem and tried all possible solution. But finally I came to know from the documentation that ngRoute module is now separated. Have a look to this link

Solution: Add the cdn angular-route.js after angular.min.js script

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular-route.js"></script>
Envelop answered 23/3, 2016 at 6:46 Comment(0)
T
5

Another trigger for this error is leaving the "." out before your "otherwise" or any other route in your route definition:

  app.config(['$routeProvider',
     function($routeProvider) {
        $routeProvider.
           when('/view1', {
              templateUrl: 'partials/view1.html',
              controller: 'Ctrl1'
           }).
           otherwise({
              redirectTo: '/viewCounts'
           });
     }]);

Mortified by a full-stop, yet again. Gotta love JS!

Travers answered 16/7, 2015 at 20:16 Comment(0)
A
4

Besides below answer, if you have this error in console ([$injector:nomod], MINERR_ASSET:22), but everything seems to work fine, make sure that you don't have duplicate includes in your index.html.

Because this error can also be raised if you have duplicate includes of the files, that use this module, and are included before the file with actual module declaration.

Apprehension answered 21/2, 2014 at 13:23 Comment(0)
M
3

If you go through the official tutorial of angularjs https://docs.angularjs.org/tutorial/step_07

Note: Starting with AngularJS version 1.2, ngRoute is in its own module and must be loaded by loading the additional angular-route.js file, which we download via Bower above.

Also please note from ngRoute api https://docs.angularjs.org/api/ngRoute

Installation First include angular-route.js in your HTML:

You can download this file from the following places:

Google CDN e.g. //ajax.googleapis.com/ajax/libs/angularjs/X.Y.Z/angular-route.js Bower e.g. bower install [email protected] code.angularjs.org e.g. "//code.angularjs.org/X.Y.Z/angular-route.js" where X.Y.Z is the AngularJS version you are running.

Then load the module in your application by adding it as a dependent module:

angular.module('app', ['ngRoute']); With that you're ready to get started!

Mendacious answered 12/1, 2015 at 14:57 Comment(0)
S
2

John Papa provided my issue on this rather obscure comment: Sometimes when you get that error, it means you are missing a file. Other times it means the module was defined after it was used. One way to solve this easily is to name the module files *.module.js and load those first.

Sommerville answered 16/2, 2017 at 19:53 Comment(0)
M
1

Its an injector error. You may have use lots of JavaScript files so the injector may be missing.

Some are here:

var app = angular.module('app', 
    ['ngSanitize', 'ui.router', 'pascalprecht.translate', 'ngResource', 
     'ngMaterial', 'angularMoment','md.data.table', 'angularFileUpload', 
     'ngMessages', 'ui.utils.masks', 'angular-sortable-view',          
     'mdPickers','ngDraggable','as.sortable', 'ngAnimate', 'ngTouch']
);

Please check the injector you need to insert in your app.js

Megavolt answered 7/7, 2017 at 7:35 Comment(0)
M
0

My problem was in the config.xml. Changing:

<access origin="*" launch-external="yes"/>

to

<access origin="*"/>

fixed it.

Mushy answered 4/2, 2015 at 18:9 Comment(0)
D
0

Sometime I have seen this error when you are switching between the projects and running the projects on the same port one after the other. In that case goto chrome Developer Tools > Network and try to check the actual written js file for: if the file match with the workspce. To resolve this select Disable Cache under network.

Diann answered 31/5, 2018 at 12:27 Comment(0)
L
0

After many months, I returned to develop an AngularJS (1.6.4) app, for which I chose Chrome (PC) and Safari (MAC) for testing during development. This code presented this Error: $injector:modulerr Module Error on IE 11.0.9600 (Windows 7, 32-bit).

Upon investigation, it became clear that error was due to forEach loop being used, just replaced all the forEach loops with normal for loops for things to work as-is...

It was basically an IE11 issue (answered here) rather than an AngularJS issue, but I want to put this reply here because the exception raised was an AngularJS exception. Hope it would help some of us out there.

similarly. don't use lambda functions... just replace ()=>{...} with good ol' function(){...}

Lobeline answered 9/7, 2018 at 7:13 Comment(0)
O
0

I had this problem and after check my code line by line i saw this

 <textarea class="form-control" type="text" ng-model="WallDesc" placeholder="Enter Your Description"/>

i just changed it to

     <textarea class="form-control" type="text" ng-model="WallDesc" placeholder="Enter Your Description"></textarea>

and it's worked. so check your tags.

Omsk answered 17/2, 2020 at 1:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.