title: Global variable not working in NodeJS
body: <p>I am trying to get global variables working in node.js, but it seems like I don't really understand the concept, even though my understanding matches the documentation.</p>
  
  <h1>Minimal example</h1>
  
  <p>My <strong>main.js</strong> file, which is compiled using rollup is:</p>
  
  <pre><code>global.a = 1;
  require('./core/test.js');
  </code></pre>
  
  <p>My <strong>core/test.js</strong> file is simply:</p>
  
  <pre><code>console.log(a);
  </code></pre>
  
  <p>This causes the error:</p>
  
  <blockquote>
    <p>Uncaught ReferenceError: a is not defined</p>
  </blockquote>
  
  <p>The fully compiled output is:</p>
  
  <pre><code>(function (exports) {
  'use strict';
  
  var commonjsGlobal = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
  
  console.log(a);
  
  commonjsGlobal.a = 1;
  
  }((this.LaravelElixirBundle = this.LaravelElixirBundle || {})));
  //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjpudWxsLCJzb3VyY2VzIjpbIkM6L3dhbXAvd3d3L3V1YmMvcmVzb3VyY2VzL2Fzc2V0cy9qcy9jb3JlL3Rlc3QuanMiLCJDOi93YW1wL3d3dy91dWJjL3Jlc291cmNlcy9hc3NldHMvanMvYXBwLmpzIl0sInNvdXJjZXNDb250ZW50IjpbImNvbnNvbGUubG9nKGEpO1xyXG4iLCJnbG9iYWwuYSA9IDE7XHJcbnJlcXVpcmUoJy4vY29yZS90ZXN0LmpzJyk7XHJcbiJdLCJuYW1lcyI6WyJnbG9iYWwiXSwibWFwcGluZ3MiOiI7Ozs7O0FBQUEsT0FBTyxDQUFDLEdBQUcsQ0FBQyxDQUFDLENBQUMsQ0FBQzs7QUNBZkEsY0FBTSxDQUFDLENBQUMsR0FBRyxDQUFDLENBQUM7OyJ9
  </code></pre>
  
  <p>It seems that <code>a</code> gets defined <em>after</em> <code>console.log(a);</code>. I'm not 100% sure if this matters in JS but it could be causing the error.</p>
  
  <p>So my question is: what causes this, and what am I doing wrong here?</p>
  
  <p>Possibly relevant information: I'm using Laravel Elixir to compile everything.</p>
  
  <p>Note: Please don't post discussion on whether or not to use global variables in node.js or in general. I know it's usually a bad idea, but I have a very good reason to do it in this case.</p>
  
  <h2>Edit: Additional context</h2>
  
  <p>What I'm really trying to do is getting Zurb Foundation to work with Laravel Elixir though node.js. I installed <code>foundation-sites</code> through NPM. <code>foundation-sites</code> relies on <code>jquery</code>, which it pulls in itself. <em>However</em>, Foundation doesn't seem to follow the usual conventions of just using something like <code>var jquery = require('jquery');</code> in its own js file. It actually relies upon a global <code>jQuery</code> variable being available. Therefore I have to ensure that somehow.</p>
  
  <p>My actual file looks like this:</p>
  
  <pre><code>global.jQuery = global.$ = require('jquery');
  require('foundation-sites');
  </code></pre>
  
  <p>So if there are any Foundation/Laravel-specific anwers I would be very happy to hear them as well. Is there something I'm not getting, or did Foundation just not create their package the "right" way?</p>