Here are 2 files:

// main.js
require('./modules');
console.log(name); // prints "foobar"

// module.js
name = "foobar";

When I don't have "var" it works. But when I have:

// module.js
var name = "foobar";

name will be undefined in main.js.

I have heard that global variables are bad and you better use "var" before the references. But is this a case where global variables are good?

share|improve this question

Global variables are almost never a good thing (maybe an exception or two out there...). In this case, it looks like you really just want to export your "name" variable. E.g.,

// module.js
var name = "foobar";
// export it
exports.name = name;

Then, in main.js...

//main.js
// get a reference to your required module
var myModule = require('./module');

// name is a member of myModule due to the export above
var name = myModule.name;
share|improve this answer
    
global variables are bad - I totally agree with that. But I could be, that the module has a dependency to a variable. Is there a way to pass this variable to the other js-file via the require function? – appsthatmatter Jun 23 '13 at 11:17
    
@jjoe64 Not sure I follow what you mean. You can effectively share any value you want through the exports object. – jmar777 Jun 27 '13 at 19:37
5  
The OP is asking whether a variable can be defined in the main.js, and then used in module.js. I have the same requirement to define paths that are used over and over again. – designermonkey Apr 29 '14 at 14:00
2  
@Designermonkey In that case you're probably better off having a config object with those types of values that can also be require()'d into a given file. Note that you can just do global.foo = 'bar' and then access foo anywhere you'd like... but like I said in my original answer, that is almost never a good thing. – jmar777 May 13 '14 at 18:42
    
Thanks for that, I figured out how to do that, and it works a treat. Thanks for verifying I had the right idea :) – designermonkey May 13 '14 at 21:29

If we need to share multiple variables use the below format

//module.js
   let name='foobar';
   let city='xyz';
   let company='companyName';

   module.exports={
    name,
    city,
    company
  }

Usage

  // main.js
    require('./modules');
    console.log(name); // print 'foobar'
share|improve this answer
    
Needed this solution, thank you! – xtheking Oct 26 '17 at 20:54
    
brilliant way to share many variables between many js files. – Nicolas Guérinet Dec 2 '17 at 17:42

a variable declared with or without the var keyword got attached to the global object. This is the basis for creating global variables in Node by declaring variables without the var keyword. While variables declared with the var keyword remain local to a module.

see this article for further understanding - https://www.hacksparrow.com/global-variables-in-node-js.html

share|improve this answer
    
Do these fragments contradict? 1) "a variable declared with or without the var keyword got attached to the global object." and 2) "variables declared with the var keyword remain local to a module." – BaldEagle Dec 7 '17 at 21:06

To share variable between module you can use function to get the value of variable between the main and the modules.

//myModule.js
var mainFunction = null; //You can also put function reference in a Object or Array

function functionProxy(func){
    mainFunction = func; //Save the function reference
}

// --- Usage ---
// setTimeout(function(){
//   console.log(mainFunction('myString'));
//   console.log(mainFunction('myNumber'));
// }, 3000);

module.exports = {
    functionProxy:functionProxy
}

.

//main.js
var myModule = require('./myModule.js');
var myString = "heyy";
var myNumber = 12345;

function internalVariable(select){
    if(select=='myString') return myString;
    else if(select=='myNumber') return myNumber;
    else return null;
}

myModule.functionProxy(internalVariable);


// --- If you like to be able to set the variable too ---
// function internalVariable(select, set){
//    if(select=='myString'){
//        if(set!=undefined) myString = set;
//        else return myString;
//    }
//    else if(select=='myNumber'){
//      if(set!=undefined) myNumber = set;
//      else return myNumber;
//    }
//    else return null;
// }

You can always get the value from main.js even the value was changed..

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.