title: NodeJS - How to assign constructor to module.exports in self-executing function?
body: <p>I'm trying to assign a constructor in a self-executing function in NodeJS.  I'm pretty sure it's not working because my parameter is a variable pointing to module.exports, but I'm curious if there's a way to make it work while staying as close to the self-executing format as possible.  </p>
  
  <p>Here's how the code is being called...</p>
  
  <pre><code>var TemplateEngine = require('./templateEngine');
  templateEngine = new TemplateEngine({engine: 'swig'}); // "object is not a function"
  </code></pre>
  
  <p>Here's an example of code that works fine...</p>
  
  <pre><code>var assert = require('assert');
  var swig = require('swig');
  
  // Constructor
  var TemplateEngine = function(args) {
      assert.ok(args.engine, 'engine is required');
      var templateEngine = {};
  
      templateEngine.engine = args.engine;
  
      templateEngine.Render = function(templateString, model) {
          var result = swig.render(templateString, model);
          return result;
      };
  
      return templateEngine;
  };
  
  module.exports = TemplateEngine;
  </code></pre>
  
  <p>and here's an example of the code style I'd like to use, but which produces a "TypeError: Object is not a function" error because I'm not actually assigning to module.exports, just a variable that copied whatever it was pointing to.</p>
  
  <pre><code>(function(templateEngine) {
      var assert = require('assert');
      var swig = require('swig');
  
      templateEngine = function(args) {
          assert.ok(args.engine, 'engine is required');
          var templateEngine = {};
  
          templateEngine.engine = args.engine;
  
          templateEngine.Render = function (templateString, model) {
              var result = swig.render(templateString, model);
              return result;
          };
  
          return templateEngine;
      };
  })(module.exports);
  </code></pre>
  
  <p>Is there a way for me to use the above self-executing format and have my module export a Constructor?</p>