skip to Main Content

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:

enter image description here

2

Answers


  1. Try changing myapp.local to localhost in the /etc/hosts file, it should fix it

    Login or Signup to reply.
  2. I 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:

    Network: use –host to expose

    So I added the --host flag into scripts in the package.json file as below:

    "scripts": {
      "dev": "vite --host",
      "build": "tsc && vite build",
      "preview": "vite preview"
    }
    

    And it worked for me.
    I’ve made a few screenshots for your convenience:

    • Vite saying to use --host to expose

    • Place where I put the --host flag

    Reference: https://www.amitmerchant.com/expose-local-vite-instance-to-the-network

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search