Vite : Application works on localhost:5173 but not on 127.0.0.1:5173

huangapple go评论88阅读模式
英文:

Vite : Application works on localhost:5173 but not on 127.0.0.1:5173

问题

我的Vite.js应用在<http://localhost:5173/>上正常工作,但当我使用http://127.0.0.1:5173/时,应用无法正常运行。

我正在使用一台英特尔Mac(MacOS:Ventura 13.0.1)。

当我尝试创建一个普通的React应用时,<http://localhost:3000>和127.0.0.1:3000都能正常工作。

尝试搜索了一些StackOverflow帖子,他们要求我运行

sudo nano /etc/hosts

并将以下行添加到文件末尾:

127.0.0.1 myapp.local

但这也没有解决问题。

我的/etc/hosts文件:

Vite : Application works on localhost:5173 but not on 127.0.0.1:5173

英文:

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:

Vite : Application works on localhost:5173 but not on 127.0.0.1:5173

答案1

得分: 5

我试图从同一本地网络上的不同计算机上访问我的应用程序。据我所知,Vite默认情况下拒绝对其主机的访问。此外,它告诉我们:

网络:使用 --host 来公开

所以我在 package.json 文件中的脚本中添加了 --host 标志,如下所示:

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

对我而言这有效果。
我为您提供了一些截图以方便参考:

  • Vite 提示使用 --host 来公开

    Vite : Application works on localhost:5173 but not on 127.0.0.1:5173

  • 我放置 --host 标志的位置

    Vite : Application works on localhost:5173 but not on 127.0.0.1:5173

参考链接: https://www.amitmerchant.com/expose-local-vite-instance-to-the-network

英文:

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:

&quot;scripts&quot;: {
  &quot;dev&quot;: &quot;vite --host&quot;,
  &quot;build&quot;: &quot;tsc &amp;&amp; vite build&quot;,
  &quot;preview&quot;: &quot;vite preview&quot;
}

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

  • Vite saying to use --host to expose

    Vite : Application works on localhost:5173 but not on 127.0.0.1:5173

  • Place where I put the --host flag

    Vite : Application works on localhost:5173 but not on 127.0.0.1:5173

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

答案2

得分: 0

尝试在/etc/hosts文件中将myapp.local更改为localhost,应该可以解决问题。

英文:

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

huangapple
  • 本文由 发表于 2023年2月19日 20:54:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75500284.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定