title: How do I return an array from an external function in Node
body: <p>I have a file which contains the following:</p>
  
  <pre><code>const fs = require('fs');
  
  var loadSingleCsv = function (filename) {
    fs.readFileSync(filename, 'utf8', function (err, data) {
  
        var dataArray = data.split(/\r?\n/);
  
        dataArray.forEach((element,index, dataArray) =&gt; {
          dataArray[index]= element.split(",");
        });
        dataArray.forEach((element,index, dataArray) =&gt; {
          dataArray[index] = `${element[0]}, ${element[1]}, ${element[2]}, ${element[3]}, ${element[4]}`;
        });
  
        console.log(dataArray);  // this prints to the console as expected
  
      return dataArray;
      });
    }  
  
  module.exports = { loadSingleCsv };
  
  </code></pre>
  
  <p>When I call it from another file, the array shows as 'undefined'. Here is my code:</p>
  
  <pre><code>const loadCsv = require ('../../load-csv-file');
  
  dataArray = loadCsv.loadSingleCsv('./csv-files/rcm-data-01.csv');
  
  console.log(dataArray);
  
  </code></pre>
  
  <p>I'm assuming that this is a real newbie error, but I would appreciate any help you can give.</p>
  
  <p>Thank you.</p>