How to pass variables into NodeJS modules?

不羁的心 提交于 2019-12-30 06:08:47

问题


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';

回答1:


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

You can implement your module as a JS class like:

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



回答2:


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.




回答3:


Should work just 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 a closure that the scope isn't any longer the module itself. So that could be your problem.

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



来源:https://stackoverflow.com/questions/28380561/how-to-pass-variables-into-nodejs-modules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!