Node.js "Cannot read property 'prototype' of undefined" error - Node.js, MongoDB and Angularjs book
Solution 1
I'll post it as an answer so this question won't get marked as unanswered.
You should change:
Account.prototype.__proto__ = events.EventEmitter.prototype;
to:
Account.prototype = Object.create(events.EventEmitter.prototype);
Solution 2
process.EventEmitter
is deprecated, use require('events')
instead.
Search through all your reference files (should be in multiple files) for:
var EventEmitter = process.EventEmitter
An where ever it exists, change that line to this:
var EventEmitter = require('events')
Solution 3
var EventEmitter = process.EventEmitter
is deprecated in high version of node, use var EventEmitter = require('events')
instead
Solution 4
This compilation error because of versions conflicts of npm and node. I had the same issue in my project. I resolved after install the same compatible versions of npm and node as on another machine.
Check:
nmp -v
and
node -v.
cwmacken
Updated on June 19, 2022Comments
-
cwmacken almost 2 years
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.
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("Account balance: $%d", this.balance); } function checkOverdraw() { if (this.balance < 0) { console.log("Account Overdrawn!!!!!"); } } function checkGoal(acc, goal) { if (acc.balance > goal) { console.log("Goal Achieved!!!!!"); } } var account = new Account(); account.on("balanceChanged", displayBalance); account.on("balanceChanged", checkOverdraw); account.on("balanceChanged", function() { checkGoal(this, 1000); }); account.deposit(220); account.deposit(320); account.deposit(600); account.withdraw(1200);
When I run this I get the error.
TypeError: Object #<Account> has no method 'on' at Object.<anonymous> (/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
From my limited understanding after researching I think this means that the "on" module is not being loaded. I found a solution that suggested something similar to adding this to line 1
var events = require('events').on;
which then results in the error.
TypeError: Cannot read property 'EventEmitter' of undefined at Object.<anonymous> (/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
Following the logic from the first fix I tried implementing the same fix but with EventEmitter
var events = require('events').EventEmitter;
Hooray it looks like it worked.... or not......and now I get this error
TypeError: Cannot read property 'prototype' of undefined at Object.<anonymous> (/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
I tried adding the below code thinking why not?
var events = require('events').prototype;
and it just brings me back to the same error from before
TypeError: Cannot read property 'EventEmitter' of undefined at Object.<anonymous> (/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
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.
Cheers.