Given two modules a and b. I know that it is possible to expose a‘s functionality to another module using the module.exports. I probably do not use it correctly.
a.js
function A() { ... }
A.prototype.func = function() { ... }
function test() {
new A().func();
}
test();
module.exports = {
A : new A()
};
The test() is working correctly. But the following breaks:
b.js
var A = require("./a");
A.func(); //throws an exception
How do I export the whole A module with its functionality?
Update: executing console.log(A) over b (as a second line), does not reveal any of A‘s methods and variables.
Try this:
You won’t be able to instantiate a new
Ain b, but it seems like that’s what you want.Edit:
Or you could change
b.jsto:But this is likely not what you want.
The idea is that whatever
exportsis will be what is returned fromrequire. It’s exactly the same reference.