fs.readFileSync
doesn't add anything to the end of lines,
instead the file that you're trying to read is using CRLF
line endings, meaning that each line ends with the \r\n
sequence.
Really your file looks something like this:
line1\r\nline2\r\nline3\r\n
But your text editor will hide these characters from you.
There are two different ways you can fix this problem.
- Change the type of line endings used in your text editor
This is IDE specific but if you use Visual Studio Code you can find the option in the bottom right.

Clicking on it will allow you to change to LF
line endings, a sequence where lines are followed by a single \n
character.
- Replace unwanted
\r
characters
Following on from your example we can use .replace
to remove any \r
characters.
let names = fs.readFileSync(namefile)
.toString()
.replace(/\r/g, "")
.split("\n")
More on line endings