I have a directory structure like this:
node_modules/mymodule/index.js :
module.exports = require('./lib/mymodule');
node_modules/mymodule/lib/mymodule.js
var A = require('./a'),
function MyModule(){
var self = this;
self.version = '0.0.1';
}
/**
* Expose MyModule
*/
module.exports = MyModule;
/**
* Expose A
*/
exports.A = A;
node_modules/mymodule/lib/a.js
function A(options) {
console.log('A::constructor()');
};
User.prototype.test = function() {
return "your test was successful";
};
module.exports = A;
My issue come with trying to instantiate A. In the node prompt I get this:
> var MyModule = require('mymodule');
undefined // why do I get undefined here ?
> var mymodule = new MyModule();
undefined
> var A = MyModule.A;
undefined
> var a = new A();
TypeError: undefined is not a function
at repl:1:9
at REPLServer.eval (repl.js:80:21)
at repl.js:190:20
at REPLServer.eval (repl.js:87:5)
at Interface.<anonymous> (repl.js:182:12)
at Interface.emit (events.js:67:17)
at Interface._onLine (readline.js:162:10)
at Interface._line (readline.js:426:8)
at Interface._ttyWrite (readline.js:603:14)
at ReadStream.<anonymous> (readline.js:82:12)
Change node_modules/mymodule/lib/mymodule.js
to:
var exports = module.exports = {
version: '0.0.1'
}
exports.A = require('./a');
You can only have one module.exports
;)
The node REPL will print the return value of the command, which in all of those cases is undefined. That doesn't mean your variables are undefined.
Try:
> var A = MyModule.A;
then
> A