Reading local text file into a JavaScript array [duplicate] Reading local text file into a JavaScript array [duplicate] javascript javascript

Reading local text file into a JavaScript array [duplicate]


Using Node.js

sync mode:

var fs = require("fs");var text = fs.readFileSync("./mytext.txt");var textByLine = text.split("\n")

async mode:

var fs = require("fs");fs.readFile("./mytext.txt", function(text){    var textByLine = text.split("\n")});

UPDATE

As of at least Node 6, readFileSync returns a Buffer, so it must first be converted to a string in order for split to work:

var text = fs.readFileSync("./mytext.txt").toString('utf-8');

Or

var text = fs.readFileSync("./mytext.txt", "utf-8");