I have two modules, both of which need to access a shared array. I solve this by having a module which just consists of the following:
sharedArray.js
module.exports = [];
In the modules I use it like this:
module1.js
var arr = require('./sharedArray');
function addSomething() {
// Add something to arr
}
module2.js
var arr = require('./sharedArray');
function doSomething() {
// Use arr for something
}
This works but feels wrong (useless empty module) and that I'm missing something obvious.
Is there a better way to do it or is this actually how you solve it?
When we want to export a single class/variable/function from one module to another module, we use the module. exports way. When we want to export multiple variables/functions from one module to another, we use exports way.
The main purpose of module. exports is to achieve modular programming. Modular programming refers to separating the functionality of a program into independent, interchangeable modules, such that each contains everything necessary to execute only one aspect of the desired functionality.
Every module can have two different types of export, named export and default export. You can have multiple named exports per module but only one default export.
If a module's primary purpose is to house one specific export, then you should consider exporting it as a default export. This makes both importing and actually using the import a little easier. Further, TypeScript recommends named exports when there are multiple things to export [4].
It is not a bad practice. It is just one of several options for sharing data among modules. There is nothing "wrong" with doing it the way you are doing it.
When sharing data among modules in node.js, you basically have three choices:
You can have one module that assigns the data to the global namespace when loaded.
You can have a method of some module that returns a reference to the data. The method could be the module constructor or some other method.
You can make the data be part of a static data structure that is in the module exports. This is essentially what you are doing now where you just make the whole exports be your array.
All three are perfectly legitimate ways of sharing the data.
The global data has the typical downsides of using globals (impacts the global namespace and potentially conflicts with a module that isn't even trying to use this particular data).
You could use the 2nd option when you were doing many other things with the module also or if you were generating the data upon demand rather than declaring it statically.
For the third option, if you made the module return an object and had the array be one property in that object that would make your module more extensible because then you could have other shared elements in that module also. But, it works as you have it, it just isn't very extensible.
I personally would probably go for the more extensible option so you could have other shared items there also:
sharedData.js
module.exports = {
myData: [...],
myOtherData: [...]
};
module1.js
var arr = require('./sharedData').myData;
function addSomething() {
// Add something to arr
}
module2.js
var arr = require('./sharedData').myData;
function addSomething() {
// Add something to arr
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With