Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read txt file's lines in JS (Node.js) [duplicate]

I want to read a text file (.txt) using Node.js. I need to push each of text's lines into array, like this:

a
b
c

to

var array = ['a', 'b', 'c'];

How can I do this?

like image 758
JustLogin Avatar asked May 24 '13 09:05

JustLogin


1 Answers

You can do this :

var fs  = require("fs");
var array = fs.readFileSync(path).toString().split('\n');

Or the asynchronous variant :

var fs  = require("fs");
fs.readFile(path, function(err, f){
    var array = f.toString().split('\n');
    // use the array
});
like image 175
Denys Séguret Avatar answered Sep 21 '22 11:09

Denys Séguret