Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass variables into NodeJS modules?

In one of my JS files I include another one. How can I set variables in the included module?

I thought doing something like this would work

var mymodule = require('mymodule.js');
mymodule.myvariable = 'test';

And then in mymodule

this.myvariable === 'test';

But this doesn't work, it's undefined. What are the various options for passing a value into a module? I could just add the variable as a parameter to every function I call in mymodule, but that isn't ideal.

Is there a way to do it without globals, so that I can set the variables independently in various required modules, like this?

var mymodule1 = require('mymodule.js');
var mymodule2 = require('mymodule.js');
mymodule1.myvariable = 'test1';
mymodule2.myvariable = 'test2';
like image 926
user779159 Avatar asked Feb 07 '15 09:02

user779159


People also ask

What is .env file in node JS?

The dotenv package for handling environment variables is the most popular option in the Node. js community. You can create an. env file in the application's root directory that contains key/value pairs defining the project's required environment variables.

How modules are loaded in node JS?

Loading Core Modulesvar module = require('module_name'); As per above syntax, specify the module name in the require() function. The require() function will return an object, function, property or any other JavaScript type, depending on what the specified module returns.


1 Answers

The problem with what you were doing is that you set the variable after importing, but this.myvariable === 'test'; was being called when the module was imported, before your variable was set.

You can have your module export a function and then call the function when you import, passing your variable as an argument.

module.exports = function(myVar) {
  var myModule = {
    // has access to myVar 
    ...
  };

  return myModule; 
};

When you import,

var myModule = require('myModule')(myVar);

If you use this method, keep in mind that you get a different instance of your module wherever you import, which may not be what you want.

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.myVar = can set you up for confusion later.

module.exports = {
  myVar: null,

  setMyVar: function(myVar) {
    this.myVar = myVar;
  },

  ...

};

In this case you're accessing the same instance of the model wherever you import it.

Edit in response to comment

In the first option you show where you get a different instance each time, how can I export multiple functions that each share the same myVar? If that module exports 5 functions each that need myVar, can I set it in one place like at import time rather than passing it into each function?

Not entirely sure if I understand what you're describing, but you could do something like this:

module.exports = function(myVar) {
  var modules = {};
  modules.someModule = {...};
  modules.anotherModule = {...};
  ...

  return modules;
};

Each of these sub-modules would have access to the same myVar. So you would import as above and the result would be an object containing each of your five modules as properties. I can't say whether this is a good idea, it's getting pretty convoluted, but maybe it makes sense for your situation.

like image 161
Adam Stone Avatar answered Oct 12 '22 12:10

Adam Stone