'Node fileSync() returning incorrect size on Windows due to newline differences

I want to find the size of a text file in bytes, however, the standard method of fs.statSync("/path/to/file") returns the incorrect size, seemingly due to newlines. For example, a 461 byte file with 5 Unix-style \n newlines shows 466 bytes, presumably because it's silently converting the newlines to \r\ns before counting. The same command on the same file gives the correct result on Linux (but not WSL2), and Windows File Explorer shows the correct byte count.

My node version is 17.0.1, and I'm on Windows 10.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source