fs.readFileSync() to create a Buffer from a file.buf.toString(encoding) function.String.prototype.split(\n) to create an array of lines from the contents of the file.const fs = require('fs'); const readFileLines = filename => fs .readFileSync(filename) .toString('UTF8') .split('\n');
/* contents of test.txt : line1 line2 line3 ___________________________ */ let arr = readFileLines('test.txt'); console.log(arr); // ['line1', 'line2', 'line3']
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️