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?
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
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With