英文:
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文件:
英文:
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:
答案1
得分: 5
我试图从同一本地网络上的不同计算机上访问我的应用程序。据我所知,Vite默认情况下拒绝对其主机的访问。此外,它告诉我们:
网络:使用 --host 来公开
所以我在 package.json
文件中的脚本中添加了 --host
标志,如下所示:
"scripts": {
"dev": "vite --host",
"build": "tsc && vite build",
"preview": "vite preview"
}
对我而言这有效果。
我为您提供了一些截图以方便参考:
-
Vite 提示使用
--host
来公开 -
我放置
--host
标志的位置
参考链接: 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:
"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
答案2
得分: 0
尝试在/etc/hosts
文件中将myapp.local
更改为localhost
,应该可以解决问题。
英文:
Try changing myapp.local
to localhost
in the /etc/hosts
file, it should fix it
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论