node module of modules multiple calling directions

Ok, the actual final reason of this is to have a folder well ordered of modules (in this case, functions). The Folder structure should be something like this:

  • /app.js
  • /node_modules/
    • /mainfunction.js
    • /functions/
      • /func1.js
      • /func2.js
      • /func3.js
      • /func4.js
      • /...

mainfunction.js:

exports.func1 = require("./functions/func1");
exports.func2 = require("./functions/func2");
exports.func3 = require("./functions/func3");
exports.func4 = require("./functions/func4");

So, from app.js or any other part of the app i can use these functions just implementing mainfunction.js like this:

var f = require("mainfunction"); //And also require the others cores module 'path, fs,..."
//then you can call the module directly.
f.func1(input)
//Or store it in a variable
var area = (new) f.func3(width)  //sometimes **new** is needed when there is no input.

This info is avaliable in Node.js API.

The problems start now.

Some modules require others to work, there are many relationships like these examples:

  1. Func2 require Func1 response
  2. Func3 require Func2 response and apart of it Func1 (so Func1 is required twice)
  3. Func4 require a core module (fs, path, ...) and others Functions.

Do i have to redeclare all the variables that every function (independent modules) needs?

example Func5.js:

var answer;
var func2 = require("./func2");            //redeclare modules needed
var fs = require("fs");                    //redeclare core modules needed

module.exports = function(input) {         //do some stuff
    fs.exist("x.html", function (exist) {
        if(exist){
            answer = func2(input);
        }
     }
     return answer;
}

I dont want to redeclare the module necessary i just want to use "f.funcX(input)" to use a function in any scope, even inside the module. Is this possible?

Any suggestion is well received. Thanks.

Another supposed option (Edited)

Ok, modules self-contained wins, but what about this? (i do not even know if this works).

Same folders and files structure. But mainfunction.js has:

module.exports = {
    require("./functions/func1"),
    require("./functions/func2"),
    require("./functions/func3"),
    require("./functions/func4"),
    ...
}

And every Function file has the same but without redeclared modules, having "exports.functX" instead, and to call the others modules, uses the name of that function (module).

This might not work, it is just an idea.

Func3 require Func2 response and apart of it Func1 (so Func1 is required twice)

As it turns out, Node's module loading system does not require Func1 twice in this situation. The process will cache Func1's exports and when required a second time, simply returns the original instantce of Func1.

Do I have to redeclare all the variables that every function (independent modules) needs?

You don't have to, but it's generally a good idea. This is, by no means, a bad thing, since each module, in fact, should be independent of one another like this; if each module does something different, then it wouldn't make sense to have every function in every module in your project anyway.

Universal loading is technically possible, however. One possible configuration would be to load each module into the global namespace(process), but in the words of Douglas Crockford, "Globals are evil." The module will be available anywhere in the Node process, even in modules that have nothing to do with yours. In essence, the require pattern that you see everywhere is, in fact, the best way to load dependencies in node.

Ok, modules self-contained wins, but what about this? (i do not even know if this works). [...]

I think you mean this?

module.exports = {
    func1: require("./functions/func1"),
    func2: require("./functions/func2"),
    func3: require("./functions/func3"),
    func4: require("./functions/func4"),
    ...
}

This is generally a good pattern if you have a module that has a lot of independent submodules to expose to whatever required it; however, it would not be possible to use such a pattern to allow a project-level namespace because Func1 would require mainfunction.js would require Func1, and so on forever. (This is known as a circular dependency.)

In short, don't worry about being repetitive in requiring modules. In the end, the standard pattern is not repetitive, because each module will have different requirements and dependencies, nor will it let a module run more than once, as explained above.

Good luck in your Node.js endeavors!