How to pass variables to NodeJS modules?

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

I thought something like this would work

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

And then in mymodule

 this.myvariable === 'test'; 

But this does not work, it is undefined . What are the different options for passing a value to a module? I could just add a variable as a parameter for every function that I call in mymodule, but that is not ideal.

Is there a way to do this without globals, so that I can set the variables myself in different required modules?

 var mymodule1 = require('mymodule.js'); var mymodule2 = require('mymodule.js'); mymodule1.myvariable = 'test1'; mymodule2.myvariable = 'test2'; 
+5
source share
3 answers

NodeJS require() will always load the module once, so you will need to implement the scope in your module, where different instances of the module can exist with their own internal state.

You can implement your module as a JS class, for example:

 var MyModule = function(){}; MyModule.prototype.someFunction = function(params){ this.someVar = params; } MyModule.prototype.anotherFunction = function(key, value){ this[key] = value; } module.exports = MyModule; 

Then in your code

 var MyModule = require('MyModule'); someModule = new MyModule(); // treat someModule like an object with its getters/setters/functions 
+13
source

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

You can configure the module to function, and then call the function upon import, passing the variable as an argument.

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

Upon import

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

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

If you want to set module values ​​from outside the module, a good option is for your module to export the object using the setter method and use it to set the value of the variable as an object property. This makes it clearer that you want this value to be customizable, while just executing myModule.myVar = can lead to confusion later on.

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

In this case, you get access to 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 every time, how can I export several functions, each of which has the same MYVAR? If this module exports 5 functions, each of which needs myVar, can I install it in one place, like during import, and not pass it to each function?

Not quite sure if I understand what you are describing, but you can do something like this:

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

Each of these submodules will have access to the same myVar. Therefore, you must import as described above, and the result will be an object containing each of your five modules as properties. I can’t say if this is a good idea, it is becoming rather confusing, but maybe this makes sense for your situation.

+7
source

It should work fine. Here is a working example:

index.js

 var module = require('./module.js'); module.myvar = 'Hello world'; module.test(); 

module.js

 module.exports = { test: function() { console.log('var is', this.myvar); } }; 

Keep in mind if you use this in closure that the scope is no longer the module itself. So this may be your problem.

Can you show me the part of the module code in which you use this?

+1
source

Source: https://habr.com/ru/post/1212856/


All Articles