Skip to content

just use portfinder when user not set port #3787

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

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions packages/@vue/cli-service/lib/commands/serve.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const {
info,
error,
hasProjectYarn,
openBrowser,
IpcMessenger
Expand Down Expand Up @@ -76,8 +77,14 @@ module.exports = (api, options) => {
const useHttps = args.https || projectDevServerOptions.https || defaults.https
const protocol = useHttps ? 'https' : 'http'
const host = args.host || process.env.HOST || projectDevServerOptions.host || defaults.host
portfinder.basePort = args.port || process.env.PORT || projectDevServerOptions.port || defaults.port
const port = await portfinder.getPortPromise()
let port = args.port || process.env.PORT || projectDevServerOptions.port
let hasUserGivenPort = true
// just use portfinder when user not set port
if (!port) {
hasUserGivenPort = false
portfinder.basePort = defaults.port
port = await portfinder.getPortPromise()
}
const rawPublicUrl = args.public || projectDevServerOptions.public
const publicUrl = rawPublicUrl
? /^[a-zA-Z]+:\/\//.test(rawPublicUrl)
Expand Down Expand Up @@ -276,6 +283,14 @@ module.exports = (api, options) => {
if (err) {
reject(err)
}
}).on('error', err => {
if (hasUserGivenPort && err.code === 'EADDRINUSE') {
console.log()
error(`Address already in use ${err.address}:${err.port}.`)
info(`You can change the port by ${chalk.cyan('--port')} option or ${chalk.cyan('vue.config.js')} or ${chalk.cyan('package.json')}.`)
process.exit(1)
}
reject(err)
})
})
})
Expand Down