title: How to export a variable from a module in JavaScript?
body: <p>According to this <a href="https://stackoverflow.com/questions/3922994/share-variables-between-files-in-node-js">post</a> we know that variables can be exported from one module in JavaScript: </p>
  
  <pre><code>// module.js
  (function(handler) {
    var MSG = {};
  
    handler.init = init;
    handler.MSG = MSG;
  
    function init() {
      // do initialization on MSG here
      MSG = ...
    }
  })(module.exports);
  </code></pre>
  
  <hr>
  
  <pre><code>// app.js
  require('controller');
  require('module').init();
  </code></pre>
  
  <hr>
  
  <pre><code>// controller.js
  net = require('module');
  
  console.log(net.MSG); // output: empty object {}
  </code></pre>
  
  <p>Those above codes are in <code>Node.js</code> and I get one <code>empty object</code> in my <code>controller.js</code>. Could you please help me figure out the reason of this?</p>
  
  <p><strong>Update1</strong></p>
  
  <p>I have updated the above codes:</p>
  
  <pre><code>// module.js
  (function(handler) {
    // MSG is local global variable, it can be used other functions
    var MSG = {};
  
    handler.init = init;
    handler.MSG = MSG;
  
    function init(config) {
      // do initialization on MSG through config here
      MSG = new NEWOBJ(config);
      console.log('init is invoking...');
    }
  })(module.exports);
  
  // app.js
  require('./module').init();
  require('./controller');
  
  // controller.js
  net = require('./module');
  net.init();
  console.log(net.MSG); // output: still empty object {}
  </code></pre>
  
  <p><strong>Output</strong>: still empty object. Why?</p>