Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I export an array with module.exports in node js?

I have a project using node.js. It's my first time using nodejs and I want to export an array to my app. Here is some code:

module.exports = { 
    var arrays = [];
    arrays[0] = 'array 0';
    arrays[1] = 'array 1';
    arrays[2] = 'array 2';
    arrays[3] = 'array 3';
    arrays[4] = 'array 4';
    var r_array = arrays[Math.floor(Math.random() * arrays.length)].toString();
}

At the end I want to use the var r_array in my app.js but I don't know how.

like image 832
Limatuz Avatar asked Dec 19 '16 08:12

Limatuz


People also ask

Can you export with module exports?

Exporting values with just the exports keyword is a quick way to export values from a module. You can use this keyword at the top or bottom, and all it does is populate the module. exports object. But if you're using exports in a file, stick to using it throughout that file.

What can you export with Module exports in node js?

Module exports are the instruction that tells Node. js which bits of code (functions, objects, strings, etc.) to “export” from a given file so other files are allowed to access the exported code.


3 Answers

You'd want to define a function which returns the randomized portion of the array:

module.exports = {
  getRArray: function() {
    var arrays = [];
    arrays[0] = 'array 0';
    arrays[1] = 'array 1';
    arrays[2] = 'array 2';
    arrays[3] = 'array 3';
    arrays[4] = 'array 4';
    return arrays[Math.floor(Math.random()*arrays.length)];
  }
};

Also you should embed the array into the function so it actually returns something.

like image 189
FatalMerlin Avatar answered Oct 14 '22 08:10

FatalMerlin


module.exports needs to be an object.

Perhaps you're looking for something more like:

var arrays = [];
arrays[0] = 'array 0';
arrays[1] = 'array 1';
arrays[2] = 'array 2';
arrays[3] = 'array 3';
arrays[4] = 'array 4';
var r_array = arrays[Math.floor(Math.random()*arrays.length)].toString();

module.exports = r_array;

Please note that this code will only be run once, and that if you're hoping to get a different random value by executing the code multiple times, that you may want to set it up more like:

module.exports = function() {
  return arrays[Math.floor(Math.random()*arrays.length)];
}

so that the Math.random() operation happens over and over.

like image 42
therobinkim Avatar answered Oct 14 '22 07:10

therobinkim


You have two files, one is array.js and the other app.js. Consider doing this in array.js file:

const Array= [
    'array 0',
    'array 1',
    'array 2',
    'array 3',
    'array 4']
   
exports.Array = Array 

Now in your app.js do the following:

const { Array } = require("./array")

const randomArr = Array[Math.floor(Math.random()*Array.length)]
console.log(randomArr)
 // I used randomArr  instead of r_array because of JS conventions.
like image 24
anonymoustafa Avatar answered Oct 14 '22 07:10

anonymoustafa