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

add pretty message if port already in use(#927) #932

Merged
merged 10 commits into from
Feb 1, 2017
Merged
Show file tree
Hide file tree
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
16 changes: 13 additions & 3 deletions bin/next-dev
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
import 'source-map-support/register'
import { resolve, join } from 'path'
import parseArgs from 'minimist'
import { existsSync } from 'fs'
import { existsSync, readFileSync } from 'fs'
import Server from '../server'
import { printAndExit } from '../lib/utils'
import pkgUp from 'pkg-up'

process.env.NODE_ENV = process.env.NODE_ENV || 'development'

Expand Down Expand Up @@ -61,6 +62,15 @@ srv.start(argv.port)
}
})
.catch((err) => {
console.error(err)
process.exit(1)
if (err.code === 'EADDRINUSE') {
let errorMessage = `Port ${argv.port} is already in use.`
const pkgAppPath = pkgUp.sync('.')
const appPackage = JSON.parse(readFileSync(pkgAppPath, 'utf8'))
const nextScript = Object.entries(appPackage.scripts).find(scriptLine => scriptLine[1] === 'next')
if (nextScript) errorMessage += `\nUse \`npm run ${nextScript[0]} -- -p <some other port>\`.`
console.error(errorMessage)
} else {
console.error(err)
}
process.nextTick(() => process.exit(1))
})
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"mkdirp-then": "1.2.0",
"mz": "2.6.0",
"path-match": "1.2.4",
"pkg-up": "1.0.0",
"react": "15.4.2",
"react-dom": "15.4.2",
"react-hot-loader": "3.0.0-beta.6",
Expand Down
8 changes: 4 additions & 4 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ export default class Server {
await this.prepare()
this.http = http.createServer(this.getRequestHandler())
await new Promise((resolve, reject) => {
this.http.listen(port, (err) => {
if (err) return reject(err)
resolve()
})
// This code catches EADDRINUSE error if the port is already in use
this.http.on('error', reject)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why we need to do this?
I think this change has nothing to do with this PR.
If so, remove this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arunoda

this.http.listen(port, (err) => {

This code does not catch the error when next starting listen port which already in use

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay.
Then add that as a comment.

this.http.on('listening', () => resolve())
this.http.listen(port)
})
}

Expand Down