In Node.js, when I want to read the lines of a file into an array, I do:
fs.readFileSync(filename).split('\r\n')
but this doesn't work on Linux. On Linux, I can do:
fs.readFileSync(filename).split('\n')
but this doesn't work on Windows. I can also do:
fs.readFileSync(filename).split(/[\r\n]+/)
which works on both systems, but hard to read.
Is there a simple, system-independent way to read a file into an array of lines in Node.js?
You can use constant for such needs:
var nl = require('os').EOL;
So it would look like:
var nl = require('os').EOL;
fs.readFileSync(filename).split(nl);
Or you could use just RegExp:
fs.readFileSync(filename).split(/\r?\n/)