title: Dynamic require in RequireJS, getting "Module name has not been loaded yet for context" error?
body: <p>Is there a way to define a module that "dynamically" load other modules in RequireJS? If yes, how the optimizer (r.js) understands how/when a module has to be included? </p>
  
  <p>For example, let <code>dynModules</code> a module which defines name/path pairs:</p>
  
  <pre><code>define([], function () {
      return ['moduleA', 'moduleB']; // Array of module names
  });
  </code></pre>
  
  <p>Another module is going to load modules dynamically, based on the array. This will <strong>not work</strong>:</p>
  
  <pre><code>define(['dyn_modules'], function (dynModules) {
      for(name in dynModules) {   
          var module = require(path); // Call RequireJS require
      }
  
      // ...
  });
  </code></pre>
  
  <p>... gives me:</p>
  
  <blockquote>
    <p>Uncaught Error: Module name "moduleA" has not been loaded yet for
    context: _. Use require([])
    <a href="http://requirejs.org/docs/errors.html#notloaded" rel="noreferrer">http://requirejs.org/docs/errors.html#notloaded</a></p>
  </blockquote>
  
  <p>I can <strong>solve the error</strong>, but it's not "dynamic" anymore:</p>
  
  <pre><code>define(['dyn_modules', 'moduleA', 'moduleB'], function (dynModules) {
      for(name in dynModules) {   
          var module = require(path); // Call RequireJS require
      }
  
      // ...
  });
  </code></pre>