vue-cli: Vue thinks local IP address is global, causing several issues
Version
3.0.0-rc.2
Reproduction link
https://github.com/kissge/vue-cli-bug
Steps to reproduce
- Using
vue createto start a project, on a remote machine (say, AWS EC2 instance) - Run
npm run serve
What is expected?
- Global IP address is shown in console
- HMR works
- App is accessible from the internet via http://mydomain.example.com/:8080
What is actually happening?
- Local IP address is shown in console
- HMR fails because sockjs tries to connect to local IP address
- App cannot be accessed from outside the network, so I have to use tricks like ssh port forwarding
I also tried tweaking webpack config from vue.config.js, with no luck.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 7
- Comments: 28 (6 by maintainers)
the devServer options have their own options property in vue.config.js because we process them internally before handing them to the devserver:
So first, a small update. the
devServer.publicsetting should allow you to set the public host to0.0.0.0:8080It doesn’t do that for me. An that’s a different scenario that yours, where you do access from an external host, right?
I think
vue create my_project_nameshould also generate avue.config.jsfile at the root of the project with the default configuration (and maybehostCheckoption set to true)Cases like this happen if you are running another app (e.g. Laravel via the artisan CLI) concurrently on your network that uses the same port as your Vuejs app. So basically it is listening to both your local and network IP when running the app in your dev environment.
if you are working on Vue CLI, may not be able to configure webpack dev server directly. instead, locate your vue.config.js file and use the sample below.
I think setting
devServer.publicto0.0.0.0is a misconfiguration. The linked docs say:Based on my testing, the address given for
devServer.publicis what the (browser) client will use when attempting to open a websocket connection for HMR. So if you set it to0.0.0.0it won’t be able to open the websocket because that’s not a routable IP.Something that can be confusing is the auto-detection can end up using an IP from a purely local network adapter. For example, the
yarn servedefaults give me:That
10.0.75.1IP is forDockerNAT. It’s only accessible from my machine. I’ve always thought you can bind tolocalhostand0.0.0.0reliably and beyond that you need user specific config. Where does the10.0.75.1get detected?The
NetworkIP (10.0.75.1) is the same IP that is used (by default) for the HMR websocket for me. Where is that default getting set? Ifvue-cli-service serveis setting it, I don’t think it should. Thewindow.locationstrategy described in the Webpack docs seems like the most reliable way to do it. If that doesn’t work, I can’t think of a way you’d be able to figure it out (reliably) without having the user specify it. Is there another way?What exactly is the
Networkaddress being reported? When I setdevServer.publicto an alternate IP, the HMR websocket connection uses the newly set IP (as expected), butyarn servestill reportsNetwork: http://10.0.75.1:8080/.It also seems like the server binds to
0.0.0.0even though the aboveApp running atmessage gives the impression it bound to 2 specific interfaces. By default, I can access the server from another PC on my LAN. If I setdevServer.hostto0.0.0.0, there’s no change:gives:
The app is still accessible from my LAN. If I set
devServer.hosttolocalhostsomething doesn’t like it:However, the app is only accessible at
localhostwhich makes sense since I sethosttolocalhost.Is the goal to avoid giving the user
http://0.0.0.0:8080as an address? If so, maybe you could do something like this (ignoring https options):Then, if
devServer.hostis0.0.0.0(this always includeslocalhost):Otherwise:
The
--publicoption described in the Webpack Docs doesn’t seem to pass through on the CLI. Is it supposed to? Ideally the OPs case would be solved by (note: this does not work currently):with the output:
@kissge Try this
vue.config.js:Note that you must be able to connect to
mydomain.example.comon port8080.@LinusBorg A decent use case for wanting to do this is to have a site / app running on an alternate device during development. For example, at the moment I’m playing around with an app on a Raspberry Pi. The screen is fairly low resolution (800x480), the color reproduction isn’t comparable to the monitor I use for development, and I want to test the touch input. It’s useful to have it plugged in beside me auto-reloading as I make changes.
Same issue with @vue/cli-service@3.6.0
So, the inference for public network API has its limitations, but you can always explicitly specify the public URL to use with
devServer.publicinvue.config.js.In ccc90c98 I’ve also added:
vue-cli-service serve --public your/url:portWanted to leave a comment here for folks arriving from Google looking to get this working with an ngrok tunnel. Ngrok configuration is standard http tunnel. The final working dev server config for us ended up being
This ought to allow you to access via tunnel (including https) with hot reloading working and no CORS issues.
Not sure why yet, but after upgrade to
@vue/cli-serviceto3.5.3start gettingon 3.5.0 was working fine without touching
vue.config.jsMaybe i don’t get something essential but i think i encounter some behaviour with cli version4 that seems to be the same thing and nothing here on this page seems to completely help:
I do a standard
vue create, without any vue.config.js file in the root dir, andnpm run servetells me: App running at:Calling it in the browser the page gets loaded fine but the console gives me (cross-origin) warnings: xxx.xxx.xxx.xxx:8081/sockjs-node/info?t=1579649486458 could not be read, one by one, adding up.
Then, after adding a vue.config.js file in the root folder with the content suggested by phalconVee
npm run servetells me: App running at:and the warnings are gone. Still, the browser now connects to http://localhost:8081 two times for each XHR:
[HMR] Waiting for update signal from WDS… log.js:24 XHRGEThttp://localhost:8081/sockjs-node/info?t=1579650320832 [HTTP/1.1 200 OK 1ms]
XHRGEThttp://localhost:8081/sockjs-node/info?t=1579650320833 [HTTP/1.1 200 OK 2ms]
What would i have to do to have no extra network participating at all? Like with
vue init webpack ...I think this is a bug, because it occurs when creating the default template with all defaults settings too:
So I’ve run
vue create hello-world --default:and I’ve ended using Google to learn that I have to add
vue.config.jswith just single property to just remove those errors.Thanks!