My Vite.js application works normally on http://localhost:5173/ but when I am using http://127.0.0.1:5173/ the app does not work.
I am using an Intel Mac (MacOs: Ventura 13.0.1).
When I tried creating a normal react app, then http://localhost:3000 and 127.0.0.1:3000 both are working the same.
Tried searching a few stackoverflow posts where they asked me run
sudo nano /etc/hosts
And the following line to the end of the file:
127.0.0.1 myapp.local
But that does not solve the issue as well.
My /etc/hosts file:
2
Answers
Try changing
myapp.local
tolocalhost
in the/etc/hosts
file, it should fix itI was trying to reach my app from a different PC on the same local network. As far as I can tell Vite denies access to its host by default. Also, it tells us:
So I added the
--host
flag into scripts in thepackage.json
file as below:And it worked for me.
I’ve made a few screenshots for your convenience:
Vite saying to use
--host
to exposePlace where I put the
--host
flagReference: https://www.amitmerchant.com/expose-local-vite-instance-to-the-network