Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

in node.js, how does one file in a module see functions in another file in the module?

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.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

in node.js, how does one file in a module see functions in another file in the module?

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×