In its simplest form, here is the issue where a class will not be correctly returned exported if it is a class.
app.js
var Users = require('./users.js');
Users.find('test');
users.js
var Users = function() {
function Users() {
console.log('Users Initiated');
}
Users.prototype.find = function(username) {
console.log(username);
};
return Users;
}
module.exports = Users;
The error I receive in the console
/Users/me/nodejsClassExport/app.js:2
Users.find('test');
^
TypeError: Object function () {
function Users() {
console.log('Users Initiated');
}
Users.prototype.find = function(username) {
console.log(username);
};
return Users;
} has no method 'find'
at Object.<anonymous> (/Users/me/nodejsClassExport/app.js:2:7)
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
How do I export it correctly so it can be used without crashing?
User.findfunction, which is why you can't call it. There is afindfunction that will exist on instances created vianew User, which is a completely different thing.