title: Node.js "Cannot read property 'prototype' of undefined" error - Node.js, MongoDB and Angularjs book
body: <p>I am working through Brad Dayley's Node.js, MongoDB and Angularjs book and I'm stuck on one of his exercises (Listing 4.4). I have a simple script emitterListener.js that is as follows the script is designed to makes checks on an account.</p>
  <pre><code>var events = require('events');
  function Account() {
      this.balance = 0;
      events.EventEmitter.call(this);
      this.deposit = function(amount) {
          this.balance += amount;
          this.emit('balanceChanged');
      };
      this.withdraw = function(amount) {
          this.balance -= amount;
          this.emit('balanceChanged');
      };
  }
  Account.prototype._proto_ = events.EventEmitter.prototype;
  
  function displayBalance() {
      console.log(&quot;Account balance: $%d&quot;, this.balance);
  }
  
  function checkOverdraw() {
      if (this.balance &lt; 0) {
          console.log(&quot;Account Overdrawn!!!!!&quot;);
      }
  }
  
  function checkGoal(acc, goal) {
      if (acc.balance &gt; goal) {
          console.log(&quot;Goal Achieved!!!!!&quot;);
      }
  }
  var account = new Account();
  
  account.on(&quot;balanceChanged&quot;, displayBalance);
  account.on(&quot;balanceChanged&quot;, checkOverdraw);
  account.on(&quot;balanceChanged&quot;, function() {
      checkGoal(this, 1000);
  });
  account.deposit(220);
  account.deposit(320);
  account.deposit(600);
  account.withdraw(1200);
  </code></pre>
  <p>When I run this I get the error.</p>
  <pre><code>TypeError: Object #&lt;Account&gt; has no method 'on'
  at Object.&lt;anonymous&gt; (/Users/506132/web/emitterListener.js:35:9)
  at Module._compile (module.js:456:26)
  at Object.Module._extensions..js (module.js:474:10)
  at Module.load (module.js:356:32)
  at Function.Module._load (module.js:312:12)
  at Function.Module.runMain (module.js:497:10)
  at startup (node.js:119:16)
  at node.js:906:3
  </code></pre>
  <p>From my limited understanding after researching I think this means that the &quot;on&quot; module is not being loaded. I found a solution that suggested something similar to adding this to line 1</p>
  <pre><code>var events = require('events').on;
  </code></pre>
  <p>which then results in the error.</p>
  <pre><code>TypeError: Cannot read property 'EventEmitter' of undefined
  at Object.&lt;anonymous&gt; (/Users/506132/web/emitterListener.js:16:35)
  at Module._compile (module.js:456:26)
  at Object.Module._extensions..js (module.js:474:10)
  at Module.load (module.js:356:32)
  at Function.Module._load (module.js:312:12)
  at Function.Module.runMain (module.js:497:10)
  at startup (node.js:119:16)
  at node.js:906:3
  </code></pre>
  <p>Following the logic from the first fix I tried implementing the same fix but with EventEmitter</p>
  <pre><code>var events = require('events').EventEmitter;
  </code></pre>
  <p>Hooray it looks like it worked.... or not......and now I get this error</p>
  <pre><code>TypeError: Cannot read property 'prototype' of undefined
  at Object.&lt;anonymous&gt; (/Users/506132/web/emitterListener.js:17:48)
  at Module._compile (module.js:456:26)
  at Object.Module._extensions..js (module.js:474:10)
  at Module.load (module.js:356:32)
  at Function.Module._load (module.js:312:12)
  at Function.Module.runMain (module.js:497:10)
  at startup (node.js:119:16)
  at node.js:906:3
  </code></pre>
  <p>I tried adding the below code thinking why not?</p>
  <pre><code>var events = require('events').prototype;
  </code></pre>
  <p>and it just brings me back to the same error from before</p>
  <pre><code>TypeError: Cannot read property 'EventEmitter' of undefined
  at Object.&lt;anonymous&gt; (/Users/506132/web/emitterListener.js:16:35)
  at Module._compile (module.js:456:26)
  at Object.Module._extensions..js (module.js:474:10)
  at Module.load (module.js:356:32)
  at Function.Module._load (module.js:312:12)
  at Function.Module.runMain (module.js:497:10)
  at startup (node.js:119:16)
  at node.js:906:3
  </code></pre>
  <p>What am I doing wrong here? How should I go about debugging this and where should I look? Thanks in advance for helping a newbie out.</p>
  <p>Cheers.</p>