title: Simply get each line in a .txt file into a javascript array
body: <p>Thought this wouldn't be this difficult, but I'm simply trying to get the contents of a file and store each line into an array.</p>
  
  <p>The 'file.txt' file (formatted as plain text):</p>
  
  <pre><code>file1.dwg
  file2.dwg
  file3.pdf
  file4.dwg
  file5.pdf
  </code></pre>
  
  <p>My javascript code:</p>
  
  <pre><code>function readTextFile('file.txt') {
      var rawFile = new XMLHttpRequest();
      rawFile.open("GET", 'file.txt', false);
      rawFile.onreadystatechange = function ()
      {
          if(rawFile.readyState === 4)
          {
              if(rawFile.status === 200 || rawFile.status == 0)
              {
                  var allText = rawFile.responseText;
                  alert(allText);
              }
          }
      }
      rawFile.send(null);
  }
  </code></pre>
  
  <p>The syntax errors shown in the console:
  <br>- safari: <code>SyntaxError: Unexpected string literal 'file.txt'. Expected a parameter pattern or a ')' in parameter list.</code>
  <br>- chrome: <code>Uncaught SyntaxError: Unexpected string</code></p>
  
  <p>I'm running the files on a simple live server, with the following file structure:
  <br>- file.txt
  <br>- page.html
  <br>- script.js</p>
  
  <p>The page.html just calls the script and is somewhere where I can work and open the console.</p>
  
  <p>Why are the errors occurring, and how can they be fixed? I've tried to follow the instructions from this other post, but it has also failed: <a href="https://stackoverflow.com/questions/14446447/how-to-read-a-local-text-file">How to read a local text file?</a></p>
  
  <p>Then I also tried the node solution and made my code like this:</p>
  
  <pre><code>var fs = require('fs');
  var array = fs.readFileSync('file.txt').toString().split("\n");
  for(i in array) {
      console.log(array[i]);
  }
  </code></pre>
  
  <p>I know nothing about Node but gave it a go - the errors were:
  <br>- chrome: Uncaught ReferenceError: require is not defined
  <br>- safari: ReferenceError: Can't find variable: require</p>
  
  <p>Thanks for any help here - I don't know how to fix this seemingly simple problem.</p>