bjskistad bjskistad - 1 month ago 6
Node.js Question

How to read file with \n character in it?

I read that you can read a file line by line in Node.js like this:

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


But, if the file has a
\n
character in it, it splits that too, so you don't get the file lines the right way.

Is there a way I can read a file with
\n
characters and get the files lines correctly?

For example, how would I split this file?

file
print "\n"
exec


When I run the above code, it results like this:

[ 'file', 'print "\\n"', 'exec' ]


Fix?

Answer

From comment above but it's the same as before ...

var array = require("fs").readFileSync("file").toString().split(String.fromCharCode(10));
Comments