Skip to content
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

Vite startup 3000 port occupancy problem #7016

Closed
7 tasks done
Brolly0204 opened this issue Feb 21, 2022 · 5 comments · Fixed by #8543
Closed
7 tasks done

Vite startup 3000 port occupancy problem #7016

Brolly0204 opened this issue Feb 21, 2022 · 5 comments · Fixed by #8543

Comments

@Brolly0204
Copy link

Describe the bug

My computer has started a front-end project, and the listening port is 3000. When I start another vite project, it still occupies port 3000 and does not open other ports for me. As a result, when I access the 3000 port link, I access the vite project, but the previous project can't be accessed.

Reproduction

https:/Brolly0204/vite-admin

System Info

macOS Monterey  version 12.2.1
vite: "^2.8.0"

Used Package Manager

pnpm

Logs

vite --host

Validations

@bluwy
Copy link
Member

bluwy commented Feb 21, 2022

When starting another Vite project, Vite should technically detect that 3000 is occupied and try 3001 though 🤔 That's the behaviour I get currently with macOS Monterey v12.2.1 as well.

@Brolly0204
Copy link
Author

Brolly0204 commented Feb 21, 2022

When starting another Vite project, Vite should technically detect that 3000 is occupied and try 3001 though 🤔 That's the behaviour I get currently with macOS Monterey v12.2.1 as well.

I tried and found the reason. When two projects are started, one specifies -- host and the other does not specify -- host will look like this.

@Brolly0204
Copy link
Author

When starting another Vite project, Vite should technically detect that 3000 is occupied and try 3001 though 🤔 That's the behaviour I get currently with macOS Monterey v12.2.1 as well.

image

image

@Brolly0204
Copy link
Author

Brolly0204 commented Feb 21, 2022

When starting another Vite project, Vite should technically detect that 3000 is occupied and try 3001 though 🤔 That's the behaviour I get currently with macOS Monterey v12.2.1 as well.

I used the same port service as the vite project to build the service with node, which caused a strange phenomenon.

vite

npm run dev -- --host

image

node server
localhost:3000

import { createServer } from 'http'

const httpServer = createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'application/json' });
  res.end(JSON.stringify({
    data: 'Hello World!'
  }));
})

// const host = '192.168.1.101'
const host = 'localhost'
const strictPort = false
const port = 3000
const server = function () {
  return new Promise((resolve, reject) => {
    const onError = (e: Error & { code?: string }) => {
      console.log('e', e)
      if (e.code === 'EADDRINUSE') {
        if (strictPort) {
          httpServer.removeListener('error', onError)
          reject(new Error(`Port ${port} is already in use`))
        } else {
          console.info(`Port ${port} is in use, trying another one...`)
          httpServer.listen(++port, host)
        }
      } else {
        httpServer.removeListener('error', onError)
        reject(e)
      }
    }
    httpServer.on('error', onError)
    httpServer.listen(port, host, () => {
      console.log('listening to', port)
      httpServer.removeListener('error', onError)
      resolve(port)
    })
  })
}
server()

@haoqunjiang
Copy link
Member

Seems a duplicate of #5241 (comment)

@github-actions github-actions bot locked and limited conversation to collaborators Jun 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants