I'm getting a very odd error when I try to require a node module. To illustrate the problem here is the code I am trying to require:
module.exports = (function(){
this.say = function(message) {
console.log(message);
}
})();
Now when I require this module I get 'Cannot read property 'say' of undefined when I try to use it as follows:
var person = require('./Person.js')
person.say('Hello World!');
And yet, if I define the module as follows it works fine ...
module.exports = {
say : function(message) {
console.log(message);
}
};
I even tried this notation that also worked ...
module.exports = new Person();
function Person(){
this.say = function(message) {
console.log(message);
}
};
Does anyone have any idea why the first notation doesn't work properly?