0

I have two files. I am creating the multiple object of one file into other.

module1.js

var a = 5;



var check1 = function(){
    a = a + 10;
    console.log(a);
}


exports.check = check1;

module2.js

var c = require('../module1');
var d = require('../module1');
c.check();
d.check();

The output is 15 & 25.

When you declare a variable using var, the scope is local. And i am creating two instances of module.js. So why is it getting declared at global level because the output is 15 & 25?

I need output as 15 & 15 as i want to use this variable as object level variable. What should i do?

Shobhit_Geek
  • 591
  • 9
  • 22
  • 1
    "*i am creating two instances of module.js*" - no you're not. All modules are only loaded once. – Bergi Dec 15 '15 at 07:04

2 Answers2

2

Whenever you do a require, it returns a reference to the module that has already loaded into memory if there is any. Otherwise, it'll load the module first, then return the reference to it. More info in docs.

So, both your variables are actually pointing to the same object. You can verify this by requireing any modules.

var a = require('http');
var b = require('http');
console.log(a==b); // true

That is to prevent infinite loops while loading cyclic dependencies.

Depending on your module's code, you can do several things to solve the issue.

You can move the variable definition inside your function for example:

var check1 = function(){
    var a = 5;
    a = a + 10;
    console.log(a);
}

exports.check = check1;

Or you can write a function that creates a new object each time you call it.

var create = function () {
    var a = 5;
    var check1 = function(){
        a = a + 10;
        console.log(a);
    }
    return {
        check1: check1
    }
}


exports.create = create;

And in your main file:

// or you can store require('./module1') to a single variable
var a = require('./module1').create();
var b = require('./module1').create();

a.check1();
b.check1();
Aᴍɪʀ
  • 7,623
  • 3
  • 38
  • 52
  • The first solution is quite useless as you can directly log 15 (there's no other possible outcome). The second solution is similar to the one I showed, but has a bug... – Amit Dec 15 '15 at 07:07
  • @Amit I think he is not posting his real code. In the sample code he posted, yes the first one does not make any sense. But depends on what the actual code is, it might be a solution. – Aᴍɪʀ Dec 15 '15 at 07:09
  • 2
    Try your code, see what happens (Hint: TypeError: a.check1 is not a function) – Amit Dec 15 '15 at 07:11
  • 2
    @Amit Thanks for pointing it out. I fixed it :) – Aᴍɪʀ Dec 15 '15 at 07:13
1

NodeJS module system uses caching to return same instance for all references to the same module (Reference).

This results in your code having the same object instance in c & d.

If you want to have 2 separate instances, try like this:

module1.js

function Check() {
  var a = 5;
  return {
    check: function() {
      a = a + 10;
      console.log(a);
    }
  };
}

exports.Check = Check;

module2.js

var Check = require('../module1').Check;
var c = Check();
var d = Check();
c.check();
d.check();
Amit
  • 45,440
  • 9
  • 78
  • 110