title: CommonJS / RequireJS - Circular module dependencies
body: <p>I would like to ask what is the mechanism behind the commonjs/requirejs circular module dependency resovling.</p>
  
  <p>Let me give you an example. Lets assume we have following module structure</p>
  
  <pre><code> modulea
       index.js
       submodulea_a.js
       submodulea_b.js
  
  moduleb
       index.js
       submoduleb_a.js
       submoduleb_b.js
  </code></pre>
  
  <p>where modulea/index.js and moduleb/index.js just reexports interesting functionality of sub modules. i.e.:</p>
  
  <pre><code> var aa_class = require("./submodulea_a").aaclass;
   var ab_class = require("./submodulea_b").abclass;
  
   module.exports aa_class;
   module.exports ab_class;
  </code></pre>
  
  <p>also, lets assume that in aa submodule i'll do:</p>
  
  <pre><code> var ba_class = require("moduleb").ba_class;
  </code></pre>
  
  <p>the same is valid for B but lets say that in submodule b I will do:</p>
  
  <pre><code>var aa_class = require("modulea").aa_class;
  </code></pre>
  
  <p>As you can see, there is no direct circular dependency of one class on another across modules but there is circular module dependency as modulea requires moduleb and moduleb requires modulea (better said, re-exports of something from submodules).</p>
  
  <p>How this is resolved by the node commonjs or requirejs in order to avoid stack overflow? Is there something like "late binding" or whatever? I would say no as commonjs is synchronous as far as I know.</p>
  
  <p>Thanks</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>So I did some research with module structure described and it seems it works similarly to the following process:</p>
  
  <ol>
  <li>Resolve module name (package.json, node_modules, relative paths...)</li>
  <li>Load the module code (.js file)</li>
  <li>Mark the current module it is being initialized (resolved)</li>
  <li>Prepare "empty" ( {} ) exports and publish it for the current module (other modules will use it for imports during require even if it is empty)</li>
  <li>Process loaded module (execute it) and if require is found, resolve the module in the same way as the current one</li>
  <li>Once the module execution is done, mark it as it is initialized (resolved)</li>
  </ol>
  
  <p>if the module being initialized or initialized already is required to be loaded again, the cached version of its exports is used.</p>
  
  <p>the problems which can't be resolved:</p>
  
  <p>When circular module is about to be loaded it seems there is no way how to resolve:</p>
  
  <pre><code>var aa_class = require("modulea").aa_class;
  </code></pre>
  
  <p>inside of the module B as aa_class of the module A is not available at the time when it was required as initialization of module A handed over the execution to module B before the aa_class was exported from module A. So only the one thing available to module B is empty exports object from the module A. And I can't see any way to solve this what is bad because if I would like to extend the aa_class in bb_module I am lost.</p>