How to debug angular [$injector:modulerr] errors

亡梦爱人 提交于 2019-12-08 14:26:08

问题


I have a growing angular application. From time to time, I mess things up and get an error of the type:

[$injector:modulerr] Failed to instantiate module App due to:

It is often very difficult to find the error based on the error message, especially if there are many files with various controllers etc.

Is there a way to debug these errors in either Chrome or Firefox ?


回答1:


Just to illustrate.

This is what you get when you load minified version of Angular (angular.min.js):

Error: $injector:modulerr
Module Error
Failed to instantiate module eduApp due to:
Error: [$injector:modulerr] http://errors.angularjs.org/1.4.7/$injector/modulerr?p0=e...)
    at Error (native)
    at http://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js:6:416
    at http://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js:38:184
    at m (http://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js:7:322)
    at h (http://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js:37:275)
    at http://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js:37:444
    at m (http://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js:7:322)
    at h (http://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js:37:275)
    at fb (http://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js:41:35)
    at d (http://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js:19:463

And this is what you get when you use unminified version (angular.js):

Error: $injector:nomod
Module Unavailable
Module 'ui.bootstrap' 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.

The first error is really painful to solve. The second one is much more easier.

Happy coding!




回答2:


Minified AngularJS gives me unreadable errors

... Use the unminified version in development to make it easier to debug.




回答3:


I found the following approach to be somewhat helpful. Use local angular.js file so you can modify it:

  1. Open your angular.js file
  2. Go to lines with the following code:
    message = message + '\nhttp://errors.angularjs.org/1.3.5/' +
      (module ? module + '/' : '') + code;
  3. After these two lines add:
    console.log(message);
    

Taken from here: http://www.chrisgmyr.com/2014/08/debugging-uncaught-error-injectormodulerr-in-angularjs/



来源:https://stackoverflow.com/questions/23247444/how-to-debug-angular-injectormodulerr-errors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!