DEV Community

Tonghe Wang
Tonghe Wang

Posted on • Originally published at tonghe.xyz

Don't Edit Hosts File With Nano on a Mac

TLDR: If you are using a Mac, don't edit your hosts file with Nano and save it in Mac format. It will cause bizarre bugs in places you won't expect. If you are seeing bugs on a Mac with errors saying localhost is missing, try opening the hosts file and save it with Unix line ending.


I learned this while working on a React Native project, where Expo's development server just wouldn't start up. After a long and patient wait, an elaborate error message showed up. In which the most eye-catching line said:

RangeError [ERR_SOCKET_BAD_PORT]: options.port should be >= 0 and < 65536. Received 65536.

After multiple trials, including upgrading Node.js to the latest version and upgrading Catalina to Big Sur, this bug persisted.

Taking a second look at the error message, the package in question was actually freeport-async.

Googling around did help. Although I didn't see anyone sharing a bug quite like mine, some on the internet did share how they solved a bug by editing the source code of freeport-async.

What freeport-async does is check available ports on your localhost within a specified range. It has async in its name because it returns a promise.

I didn't actually need it to find an available port on my machine. I wasn't running many servers anyway. So I commented out almost all the code. And asked freePortAsync() to return a determinate number 19000, in consistency with 19xxx ports used by Expo. It worked.

It was certainly a dirty hack. But I still felt quite good about myself.

However, there were some quirks. While I could preview my app in an iOS simulator, I couldn't see it in a web browser.

Then a few bizarre error messages flashed back in my mind. Before this, whenever I created new React projects, although the development server started up fine, I couldn't reach it at localhost:3000. Weird though, 127.0.0.1:3000 would always work.

Looking through my notes, Node.js once also gave an error messages related to localhost, saying:

getaddrinfo ENOTFOUND localhost
Error: getaddrinfo ENOTFOUND localhost
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:71:26)
Enter fullscreen mode Exit fullscreen mode

I was absolutely baffled because I've checked — and at times edited — the hosts file on my Mac a million times. There had always been an entry for localhost pointing to 127.0.0.1.

Until I found this comment by Satya in a quarter of StackExchange for Apple users:

After living with this for a long time, I figured out (by checking from the Sublime editor) that the issue with my /etc/hosts file was that the "Line Endings" on the file were set to "Mac OS". When I changed the line endings to "Unix", I am able to ping localhost in the terminal and stuff's working as expected.

Every time I needed to edit something in a protected directory, out of convenience, I had always used sudo nano <filename>. When saving the file, I'd always choose Mac Format — Why wouldn't I? I'm using a Mac after all.

I opened up hosts file and saved it with Unix-style LF line ending. Restarted the computer. All the bugs mentioned above just disappeared!

Googling keywords “React Native” or “Expo” didn't give me a solution because it was not a problem of React Native or Expo. Besides, who would've expected Mac OS to be tripped up by a config file saved with Mac line ending?

Top comments (0)