Skip to content

a problem related to the vite server port #5241

@classmatewu

Description

@classmatewu

Describe the bug

  1. a new vite app: default port is 3000
    image

  2. my koa server: this port is also 3000
    image

when I start vite app and then start koa server OR start koa server and then start vite app, no indication of port conflict.

And I use Google Chrome to access the page 127.0.0.1:3000, the page display content about the latter server content.

Reproduction

This question reminds me of PM2——master-slave mode, but vite didn't handle it well. I try to read the vite's source code, but I am worried about misunderstanding, so come here to issue. If this is a problem, can you provide solutions or fix it as soon as possible?

thanks.

System Info

@vite:   2.6.4,
@node:   14.16.0,
@koa:    2.13.3,
@Google: Chrome: 94.0.4606.71 (official version) (x86_64),
@OS: 	 macOS Catalina 10.15.7,
@JavaScript:	V8 9.4.146.18,

Used Package Manager

npm

Logs

null

Validations

Activity

haoqunjiang

haoqunjiang commented on Oct 24, 2021

@haoqunjiang
Member

Not necessarily a critical bug.
Your koa server listens to 0.0.0.0 or :: by default, not 127.0.0.1 (which is the Vite default).
As Node.js sets all sockets with the SO_REUSEADDR flag (explanation: https://medium.com/@eplawless/node-js-is-a-liar-sometimes-8a28196d56b6), it allows other sockets to reuse the same port as long as it doesn't listen to the exact same address.

The problem here is that Vite chooses to display localhost while the actually bound address is 127.0.0.1; this could be misleading in some rare edge cases, like this one.

Take my network environment as an example (may not be very accurate, I'm not a network expert):

  • In my /etc/hosts, I have localhost bound to both 127.0.0.1 and ::1, meaning that localhost resolves to ::1 when IPv6 is available;
  • My ISP and router have IPv6 configured;
  • When I start a Vite server, it listens to 127.0.0.1:3000 by default;
  • When I start the koa server, it listens to the unspecified IPv6 address ::, which also accepts connections from ::1;
  • So, when I visit localhost:3000, it's equivalent to ::1:3000 as I'm in an IPv6 environment. So I end up hitting the koa server;
  • When I visit 127.0.0.1:3000, I can still visit the Vite server.
added
p2-nice-to-haveNot breaking anything but nice to have (priority)
p3-minor-bugAn edge case that only affects very specific usage (priority)
and removed
p2-nice-to-haveNot breaking anything but nice to have (priority)
on Oct 24, 2021
haoqunjiang

haoqunjiang commented on Oct 24, 2021

@haoqunjiang
Member

So the solution could be either of the following:

  • Let the server host default to:
    • Both 127.0.0.1 and ::1 (most intuitive, but I don't know how to implement that)
    • Or ::1 when IPv6 is available, and 127.0.0.1 otherwise (most conventional, but would still surprise people sometimes)
  • Or, we don't prettify the output to display localhost when it's actually 127.0.0.1
smithyj

smithyj commented on Jun 3, 2022

@smithyj

This problem is solved,set server.host = 0.0.0.0,example config:

server: {
    host: '0.0.0.0',
  },
smithyj

smithyj commented on Jun 3, 2022

@smithyj

@sodatea This issues should closed

locked and limited conversation to collaborators on Jun 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    p3-minor-bugAn edge case that only affects very specific usage (priority)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @haoqunjiang@smithyj@classmatewu

      Issue actions

        a problem related to the vite server port · Issue #5241 · vitejs/vite