I'm attempting to run this short program from Eloquent Javascript in the section on Modules.
var weekDay = function() {}();
(function(exports) {
var names = ["Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"];
exports.name = function(number) {
return names[number];
};
exports.number = function(name) {
return names.indexOf(name);
};
})(this.weekDay = {});
console.log(weekDay.name(weekDay.number("Saturday")));
The proper output should be // -> Saturday.
It works perfectly in the browser. However, when I try to run it in the Node interpreter, I get this error:
TypeError: Cannot read property 'name' of undefined
I can only assume it has something to do with the way Node handles the exports keyword. Can somebody help me gain at least a rough understanding of this behavior?
this.weekDay. Try changing that to justweekDay.var weekDay;.thisis notglobal, butexports.thisis theexportsobject, which is pre-defined by Node.