Local variables (var any) are not exported and are not local to the module. You can define your array on the export object so that you can import it. You can also create a .json file if your array contains only simple objects.
data.js:
module.exports = ['foo','bar',3];
import.js
console.log(require('./data')); //output: [ 'foo', 'bar', 3 ]
[change]
If you need a module (for the first time), its code is executed and the export object is returned and cached. For all subsequent calls to require() , only the cached context is returned.
However, you can modify objects from the module code. Consider this module:
module.exports.arr = []; module.exports.push2arr = function(val){module.exports.arr.push(val);};
and call code:
var mod = require("./mymodule"); mod.push2arr(2); mod.push2arr(3); console.log(mod.arr);
source share