Node.js pass variable to module vs pass variable to every module function

Node.js pass variable to module vs pass variable to every module function

Problem

I’m learning node.js and interested in is there any difference between following two cases. I.E. I have some variable myvar (like db connection or just constant string “test”) that needed to be passed in many modules and submodules.

First case. Create modules, that accept that variable as a param:

submodule.js:

var option
  , submodule = {};
submodule.func = function(){
    ...
    var something = option;
    ...
}
module.exports = function(opts){
    option = opts;
    return submodule;
}

module1.js:

var option
  , submodule
  , module1 = {};
module1.func = function(){
    ...
    submodule.func();
    ...
    var something = option;
    ...
}
module.exports = function(opts){
    option = opts;
    submodule = require('./submodule')(opts);
    return module1;
}

In this case if submodule is used in several modules with same myvar value (i.e. 2 modules) submodule’s module.exports function will be called 2 times. In node.js mans it said that “Modules are cached after the first time they are loaded”. And I can’t understand is this module cached or not.

Another case: That myvar can be passed as parameter to module functions. So code will look like:

submodule.js:

function func(option){
    ...
    var something = option;
    ...
};
exports.func = func;

module1.js:

var submodule = require('./submodule');
function func(option){
    ...
    submodule.func(option);
    ...
    var something = option;
    ...
};
exports.func = func;

So the question is: Is there any difference between this two cases or they are same?

Problem courtesy of: m03geek

Solution

I’m not exactly sure what you’re asking here, but if you need to pass values into your modules you should make sure to export functions that accept parameters. When they say that a module is cached, it means that your module is only initialized once. Think of your module as an object:

var a = 1;
var b = 2;
console.log("init!");

module.exports.test = function(){
      console.log("this is a function!");
}

Here, a, b, and the first log will run only once. This is when the module is requested and is then cached. When you do a

 var example = require("module")

If it was never created it’ll initialize a, b, and do the log message. If it was already created it will just give you a reference to what you exported. Each time you call:

 example.test()

It will output: this is a function!

But you will NOT get the a, b, and the first log run again.

Think of all statements not exported as private static variables of that object.

Here is another fully working example:

app.js

var s = require("./sample");
var y = require("./sample");
s.test();
y.test();
s.test();
console.log("finished");

sample.js

var a = 1;
var b = 2;
console.log("init!");
function test() {
    console.log("here! " + a);
    a++;
}
exports.test = test;

This all outputs:

init!
here! 1
here! 2
here! 3
finished

Does this help at all?

Solution courtesy of: devshorts

Discussion

Leave a Reply

Your email address will not be published. Required fields are marked *