Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js expose variable to module?

I have read a lot of articles about how to create modules in node.js, and you can use module.exports to expose module internals to the file which includes it.. awesome!

How does this work the other way around? I'll use the following as an example :-)

 USER.JS

 function User() {
   this.property = 'value';
   this.doSomething = function() {
     var getStuff = mainFileFunction();
     // do something with getStuff
 }

 module.exports = User;

 MAIN.JS

 var myArray = [];
 myArray.push('something');
 myArray.push('something else');
 mainFileFunction() {
   for(thing in myArray) {
     return myArray[thing];
   }
 }

 var u = new user();

 log(u.property);  <---  THIS IS EXPOSED, COOL!

 u.doSomething();  <--- This will throw an error because mainFileFunction is not defined in user.js :-(

If I were to move mainFileFunction to the user file, then it still wouldn't work because the myArray array wouldn't be defined... and if I were to move that over too, I wouldn't be able to use it in other functions in main (which I want to) :-)

I'm sorry if I'm missing something really obvious here... What I want is to expose the parts of my choosing from modules I include (module.export works for that) but I also want to expose everything from the main file to all the includes..

or just expose everything to everything? is that totally messy and horrible??

Just to explain what I am trying to do here... I want to have classes defined in separate files, but I want to instantiate a bunch of them as objects in the main file and store them in arrays.. I want the objects to contain methods which can access arrays of the other object types.

Thanks guys! :-)

like image 388
Andy Phillips Avatar asked Aug 27 '13 20:08

Andy Phillips


People also ask

How can we expose Node module?

Module.exports API to expose Data to other files Node supports built-in module system. Node. js can import functionality which are exposed by other Node. js files.

How do you pass a variable in node JS?

If you want to set values of a module from outside the module, a good option is to have your module export an object with a setter method, and use that to set the value of the variable as a property of the object. This makes it more clear that you want this value to be settable, whereas just doing myModule.

What is Esmodule?

ES modules are the standard for JavaScript, while CommonJS is the default in Node. js. The ES module format was created to standardize the JavaScript module system. It has become the standard format for encapsulating JavaScript code for reuse. The CommonJS module system, on the other hand, is built into Node.


1 Answers

You can use globals, or have a proper circular dependency (requireing both files), however - this is usually a bad habit which can lead to maintainability problems in the future.

Instead, you can use dependency injection and inject doSomething into your module. This basically gives you the following for free:

  • You can test User with a simple mock implementation of doSomething later and verify the correctness of your code
  • The dependencies of a user are explicit and not implicit, which makes it obvious what a user needs.

I'll provide two implementations, one using constructor dependency injection and one with a module wide setting.

 USER.JS

 function User(dependentFunction) {
   this.property = 'value';
   this.doSomething = function() {
     var getStuff = dependentFunction();
     // do something with getStuff
   }
 }

 module.exports = User;

MAIN.JS
...
var u = new User(mainFileFunction);
u.doSomething(); // this will now work, using mainFileFunction

What happens here is fairly simple, and we know what's going on.

This can also be a module wide setting

USER.JS

function User(depFunc) {
  this.property = 'value';
  this.doSomething = function() {
    var getStuff = depFunc();
    // do something with getStuff
  }
}
function UserFactory(depFunc){ 
    return function(){
        return new User(depFunc);
    }
}
module.exports = UserFactory;

MAIN.JS

 var getUser = UserFactory(mainFileFunction);
 var u = getUser(); // will return a new user with the right function
like image 73
Benjamin Gruenbaum Avatar answered Sep 22 '22 02:09

Benjamin Gruenbaum