title: Import-Export Call Order in NodeJS
body: <p>This is a &quot;code theory&quot; question.</p>
  <p>So imagine this scenario, I am using the global namespace in a package. I have a main entrypoint file, some class files that I export, and some utility files the classes use. Here's a theoretical file structure:</p>
  <pre><code>/index.js (main entrypoint)  
  /src  
  |_/source.js (exporting function for src folder)  
  |_/utils  
    |_/headers.js
    |_/constants.js
  |_/classes
    |_/class1.js
    |_/class2.js
  </code></pre>
  <p>In constants.js I define some global variables and then export an object that uses some global variables.<br />
  <em>/src/utils/constants.js</em></p>
  <pre class="lang-js prettyprint-override"><code>const foobar = 'foo';
  global.foobar = foobar;
  
  export default {
    foo: global.foobar,
  }
  </code></pre>
  <p>In some class file I import constants.js.<br />
  <em>/src/classes/class1.js</em></p>
  <pre class="lang-js prettyprint-override"><code>import constants from '../utils/constants.js'
  
  export default class Xyzzy {
    function baz() {
      return constants.foo + 'baz'
    }
  }
  </code></pre>
  <p>And finally in the entrypoint file I import <code>source.js</code> which imports <code>/src/classes/class1</code> and <code>/src/classes/class2</code> and exports both of them, then define some global variables.<br />
  <em>/index.js</em></p>
  <pre class="lang-js prettyprint-override"><code>import source from '/src/source.js'
  
  export default function index() {
    global.foobar = 'bar'
    
    return {
      class1: source.class1,
      class2: source.class2,
    }
  }
  </code></pre>
  <p>What would be the assignment order of global.foobar, what is the final result, and most importantly <strong>why does this happen</strong>?</p>
  <p>Thanks!</p>