title: Store file data in variable Javascript
body: <p>I am trying to save the content of a file to a variable using a stream. However, the scope of the variable is not accesible from outside the function.</p>
  
  <pre><code>var data = '';
  var fileContent = '';
  var reader = fs.createReadStream(filePath);
  reader.setEncoding('UTF8');
  
  reader.on('data', function(chunk) {
     data += chunk;
  });
  
  reader.on('end', function() {
     fileContent = data;
  });
  
  return fileContent;
  </code></pre>
  
  <p>I tried to return the value inside the function but it is not returning the expected thing.</p>