Is module.export = [] wrong practice?

I have two modules that need to access a shared array. I solve this by having a module that consists only of the following:

sharedArray.js

module.exports = [];

In modules, I use it as follows:

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 it seems wrong (useless empty module) and that I am missing something obvious.

Is there a better way to do this, or is it really how you solve it?

+4
source share
1 answer

This is not a bad practice. This is just one of several options for sharing data between modules. There is nothing "wrong" in doing it the way you do it.


node.js :

  • , .

  • , . - .

  • , . , , .

.

( , ).

, , , .

, , , . , , , .


, , , :

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 
}
+5

All Articles