title: Is there any reason to define module.exports using an IIFE?
body: <p>My team doesn't have any experienced JS developers, but we are writing a library in Node and got a suggestion from a real JS developer that "We should make the js more modular - not to pollute the global namespace and to make it more readable to new-comers", and told us to do the following:</p>
  
  <pre><code>module.exports = (function(){
        return {
           nameToExpose: functionToExpose
           ...
      };
  })();
  </code></pre>
  
  <p>rather than</p>
  
  <pre><code>module.exports.nameToExpose = functionToExpose;
  </code></pre>
  
  <p>What's the point of this, if any?  The latter does not make any local declarations that would be scoped by the IIFE, and even if it did, they would be local to the module file and not global to the whole program that <code>require()</code>s it.</p>
  
  <p>Some Googling and poking about this site does not turn up any answers on this particular question, though there are many other explanations of IIFEs that I have read (and which are summarized in the above comment).  Some testing certainly reveals that the latter does <em>not</em> actually put <code>functionToExpose</code> in the global namespace, though its original name is recorded in the function type itself.</p>