Tuesday, June 4, 2024
 Popular · Latest · Hot · Upcoming
40
rated 0 times [  44] [ 4]  / answers: 1 / hits: 29341  / 11 Years ago, fri, may 24, 2013, 12:00:00

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?


More From » node.js

 Answers
18

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
});

[#78045] Thursday, May 23, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
iyannae

Total Points: 147
Total Questions: 88
Total Answers: 120

Location: Japan
Member since Sat, Jun 6, 2020
4 Years ago
;