-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Check 0.0.0.0 as well as localhost/127.0.0.1 when checking for used port #10638
Comments
When
, the current behavior is:
As long as you are accessing with the address shown on start up, it will work. |
What isn't working is that vite doens't notice that it shouldn't use port 3000. For example, running
in one terminal, and then
results in Vite starting a dev server at If the first server had been Vite server as well, the second Vite server would have noticed and chosen another port
|
I might got what you mean. You don't want Vite to hijack the 3000 port. Am I correct? |
Right, I'd like Vite to choose another port. |
It makes sense to me 👍 |
Hello @thomasballinger. We like your proposal/feedback and would appreciate a contribution via a Pull Request by you or another community member. We thank you in advance for your contribution and are looking forward to reviewing it! |
@thomasballinger I was looking at your issue but I did not reproduce, Vite started a server on a different port without any issue even with a 0.0.0.0 host. Do you still reproduce ? |
@Scttpr How did you start the first server? edit: note that new versions of Vite seem to choose a random port by default, so you have to specify port 3000. |
I did exactly this |
What operating system (I tried on mac), maybe we're seeing differences there? Anyone else see this? |
I'm on Archlinux with NodeJS 16.18.0, I tried as well with a basic :
One side and :
On the other hand and it automatically started on 3001, am I in the right scenario ? |
Ok I reproduce with NodeJS 19.0.0, according to Vite inner code I would say this issue is related to how Node is able to detect used ports from its http core lib |
I guess it didn't happen with python because python's http.server doesn't use I think it reproduced with Node.js 19 because Node.js 17+ resolves localhost to ::1 in some cases and python uses IPv4. |
This is what I suspected as well although looking through the docs, node:http doesn't seem to distinguish the hostname alias |
Can this to be merged in v3 too? |
As a workaround, you can use this package https://www.npmjs.com/package/portfinder-sync import portfinder from "portfinder-sync";
const config = defineConfig({
server: {
host: "127.0.0.1",
port: portfinder.getPort(3000),
}
});
export default config; For better typescript support you can add a declaration file. declare module "portfinder-sync" {
export function getPort(port: number): number;
} |
Description
When a developer already has a dev server running (say with Next.js) that uses 0.0.0.0:3000, Vite doesn't notice and starts a dev server at localhost:3000.
It's a special case, but a common one. It'd be great it Vite could notice this.
Possible dup of #5241, but it wasn't clear to me in that issue that the solution below was explicitly discarded.
Suggested solution
Could Vite could check 0.0.0.0 as well? edit: happy to make a PR, wanted to hear whether this is felt to be a good idea first
Alternative
In #5241 the solution is proposed #5241 (comment)
which does sound like the real fix! But for this common case it might be worth checking 0.0.0.0.
Additional context
No response
Validations
The text was updated successfully, but these errors were encountered: