title: Load modules dynamically with Require.js
body: <p>I am trying to load modules dynamically as described at the beginning of this post:</p>
  
  <p><a href="https://vickev.com/#!/article/grunt-the-perfect-tool-for-require-js" rel="nofollow">Reference link</a></p>
  
  <p>Here is my <code>scripts/main.js</code></p>
  
  <pre class="lang-js prettyprint-override"><code>require.config({
    baseUrl: 'scripts',
    paths: {
      jquery: 'lib/jquery-2.0.3'
    },
    config: {
      'main': {
        modules: ['mod1', 'mod2', 'mod3']
      }
    }
  });
  
  require(function(require, exports, module) {
    console.log("Loading modules");
    require(module.config().modules);
  });
  </code></pre>
  
  <p>When main.js gets loaded, code inside the outer require function never gets executed and "Loading modules" never gets printed to the console. Having read the AMD documentation at <a href="http://requirejs.org/docs/whyamd.html#sugar" rel="nofollow">This link</a>, I can't see what I am doing wrong. What is the proper way of dynamically loading modules defined externally in an array?</p>
  
  <p>Thanks!</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>Here is what I have now:</p>
  
  <pre><code>// main.js
  require.config({
    ...
    config: {
      'some_module': {
        modules: ['mod1']
      }
    }
  });
  
  require(['some_module'], function(some_module) {
  });
  
  // some_module.js
  define(function(require, exports, module) {
    var mods = module.config().modules;
  
    var mod;
    for (var i=0; i &lt; mods.length; i++) {
      mod = require(mods[i]);
      mod.fn_call();
    }
  });
  </code></pre>
  
  <p>When I execute <code>require(module.config().modules)</code>, <code>mod1</code> indeed gets loaded. I am not sure how to use the return value of require to call a function returned by mod1, however.</p>
  
  <p>With the code above, I get </p>
  
  <pre><code>Uncaught Error: Module name "mod1" has not been loaded yet for context: _
  http://requirejs.org/docs/errors.html#notloaded
  </code></pre>
  
  <p>How can I access the functions from the modules I am loading?</p>