Node: import object array from another js file?
express, node.js
Solution
Local variables (var whatever) are not exported and local to the module. You can define your array on the exports object in order to allow to import it. You could create a .json file as well, if your array only contains simple objects.
data.js:
module.exports = ['foo','bar',3];
import.js
console.log(require('./data')); //output: [ 'foo', 'bar', 3 ]
[Edit]
If you require a module (for the first time), its code is executed and the exports object is returned and cached. For all further calls to `require()`, only the cached context is returned.
You can nevertheless modify objects from within a modules code. Consider this module:
module.exports.arr = [];
module.exports.push2arr = function(val){module.exports.arr.push(val);};
and calling code:
var mod = require("./mymodule");
mod.push2arr(2);
mod.push2arr(3);
console.log(mod.arr); //output: [2,3]
Problem
In a file called data.js, I have a big object array: ``` var arr = [ {prop1: value, prop2: value},...] ``` I'd like to use this array into my Node.js app, but code like ``` require('./data.js') ``` doesn't help. I know how to export functions, but I'm lost when it comes to "importing" arrays. How do I add the data.js file to my app.js?