title: Node js and new when using require
body: <p>I've been trying to sort out the include of other js files within node.js. </p>
  
  <p>I've read all about the require function and other alternatives and decided to go with the require function (as the code will only ever be used on node.js, not in a browser).</p>
  
  <p>In my code I'm using prototypes to create an 'object' which I later wish to create an instance off.</p>
  
  <p>To get it to work I've been writing code like the following (lets call it vehicle.js):</p>
  
  <pre><code>var util = require('util');
  var EventEmitter = require('events').EventEmitter;
  
  module.exports = Vehicle;
  
  util.inherits(Vehicle, EventEmitter);
  
  function Vehicle(options) {
      EventEmitter.call(this);
      options = options || {};
      ...
  }
  
  Vehicle.prototype._doStartEvent = function(data) {
      this.emit('start', data);
  };
  
  Vehicle.prototype.testRun = function() {
      this._doStartEvent();
  };
  </code></pre>
  
  <p>Then in my main js (lets call it server.js), I have the following:</p>
  
  <pre><code>var test = exports;
  exports.Vehicle = require('./vehicle.js');
  
  var remoteVehicle = new test.Vehicle({address: "192.168.1.3"});
  
  remoteVehicle.on('start', function(d) {console.log('started');});
  
  remoteVehicle.testRun();
  </code></pre>
  
  <p>Now this all works fine, but I do not have a good understanding of what is going on.</p>
  
  <p>My main concern is the use of <code>var test = exports;</code> and then <code>exports.Vehicle = require(...)</code>.</p>
  
  <p>I tried just doing something like <code>var vehicle = require(...).Vehicle</code> and <code>var vehicle = require(...)</code>, with the goal of just using <code>new Vehicle</code> or similar, but I couldn't get it to work.</p>
  
  <p>Am I forced to use exports and if so why?</p>
  
  <p>Please note I've been using the AR Drone project as an example, the above code is based on how they have done their modules internally. Refer to <a href="https://github.com/felixge/node-ar-drone/blob/master/lib/Client.js" rel="nofollow">Client.js</a> and <a href="https://github.com/felixge/node-ar-drone/blob/master/index.js" rel="nofollow">index.js</a>.</p>