javascriptangularjsgruntjsyeomanyeoman-generator-angular

App module is not available, misspelled or forgot to load - after grunt build


I've built an app using yeoman's generator-angular.

I've added my code and commented-out the yeoman auto-generated code for now for reference.

When I run grunt serve everything works fine, but when i build the code (minify + concat + uglify) the vendor.js file throws an error saying my main app module is not available, even though all my dependencies are loaded fine and none of them of course is not dependant of my app.

This is how my scripts.js build block looks like in my index.html file:

<!-- build:js({.tmp,app}) scripts/scripts.js -->
<!--<script src="scripts/controllers/main.js"></script>-->
<!--<script src="scripts/controllers/about.js"></script>-->
<script src="app.templates.js"></script>
<script src="app.envConfig.js"></script>
<script src="app.js"></script>
<script src="components/auth/auth.srv.js"></script>
<script src="app.routes.js"></script>
<!-- endbuild -->

Note: app.templates.js & app.envConfig.js are separated modules I wrote & use, not part of the main app module.

This is my app.js code:

angular
  .module('my-app', [
    'ngAnimate',
    'ngAria',
    'ngCookies',
    'ngResource',
    'ngTagsInput',
    //'ngRoute',
    'ngSanitize',
    'ngTouch',
    'ui.router',
    'ui.bootstrap',
    'my.templates',
    'envConfig'
]);

I came across this answer but it looks like my code is ok.

What am I doing wrong?


Solution

  • When grunt serves the files using grunt serve, It removes all commented code (html/css/js) from what it serves to the live server, BUT, When grunt is building, it turns out it will first inject the modules it needs to build, even the commented-out ones if they are inside the build blocks.

    The concatenation process will insert the main.js & about.js controllers code before the app.js module even though it is commented out.

    So it turns out the main.js & about.js controllers will load BEFORE the main app moudle is created, thus throwing an error about it not being available.

    You should either remove the commented lines completely or take them outside of the build block.