I have this in MyClass.js:
function MyClass(name) {
this.Name = name;
}
module.exports.MyClass = MyClass;
And I have this in Main.js:
var MyClass = require('./MyClass');
var obj = new MyClass('Something');
console.log(obj.Name);
But I'm getting the error TypeError: Object is not a function happening on the 'n' of new.
How can I define a class in another file in nodejs? I feel like this should have worked just fine, but it doesn't.
You said
module.exports.MyClass = MyClass;
which means
var MyClass = require('./MyClass').MyClass; // MyClass attached to the exports
If you want it to be directly available on the require you'd need to do
module.exports = MyClass;