in node.js, how does one file in a module see functions in another file in the module?
Problem
in a Module containing two files in ./modx
: modx.js
and helper.js
:
./modx/package.json:
{ "name" : "module x",
"main" : "./modx.js" }
./modx/helper.js:
function subFunc() { }
./modx/modx.js:
exports.mainFunc = function() {
var x = subFunc();
}
how do I make subFunc()
in helper.js
visible to modx.js
when both are in the modx
module?
Problem courtesy of: cc young
Solution
The only object in script A
visible from script B
is module.exports
. Adding objects/functions to module.exports
(just like you did it with mainFunc
) makes them visible from the outside. There is no other way.
Solution courtesy of: freakish
Discussion
View additional discussion.