File tree Expand file tree Collapse file tree 3 files changed +18
-7
lines changed Expand file tree Collapse file tree 3 files changed +18
-7
lines changed Original file line number Diff line number Diff line change 22import 'source-map-support/register'
33import { resolve , join } from 'path'
44import parseArgs from 'minimist'
5- import { existsSync } from 'fs'
5+ import { existsSync , readFileSync } from 'fs'
66import Server from '../server'
77import { printAndExit } from '../lib/utils'
8+ import pkgUp from 'pkg-up'
89
910process . env . NODE_ENV = process . env . NODE_ENV || 'development'
1011
@@ -61,6 +62,15 @@ srv.start(argv.port)
6162 }
6263} )
6364. catch ( ( err ) => {
64- console . error ( err )
65- process . exit ( 1 )
65+ if ( err . code === 'EADDRINUSE' ) {
66+ let errorMessage = `Port ${ argv . port } is already in use.`
67+ const pkgAppPath = pkgUp . sync ( '.' )
68+ const appPackage = JSON . parse ( readFileSync ( pkgAppPath , 'utf8' ) )
69+ const nextScript = Object . entries ( appPackage . scripts ) . find ( scriptLine => scriptLine [ 1 ] === 'next' )
70+ if ( nextScript ) errorMessage += `\nUse \`npm run ${ nextScript [ 0 ] } -- -p <some other port>\`.`
71+ console . error ( errorMessage )
72+ } else {
73+ console . error ( err )
74+ }
75+ process . nextTick ( ( ) => process . exit ( 1 ) )
6676} )
Original file line number Diff line number Diff line change 7070 "mkdirp-then" : " 1.2.0" ,
7171 "mz" : " 2.6.0" ,
7272 "path-match" : " 1.2.4" ,
73+ "pkg-up" : " 1.0.0" ,
7374 "react" : " 15.4.2" ,
7475 "react-dom" : " 15.4.2" ,
7576 "react-hot-loader" : " 3.0.0-beta.6" ,
Original file line number Diff line number Diff line change @@ -119,10 +119,10 @@ export default class Server {
119119 await this . prepare ( )
120120 this . http = http . createServer ( this . getRequestHandler ( ) )
121121 await new Promise ( ( resolve , reject ) => {
122- this . http . listen ( port , ( err ) => {
123- if ( err ) return reject ( err )
124- resolve ( )
125- } )
122+ // This code catches EADDRINUSE error if the port is already in use
123+ this . http . on ( 'error' , reject )
124+ this . http . on ( 'listening' , ( ) => resolve ( ) )
125+ this . http . listen ( port )
126126 } )
127127 }
128128
You can’t perform that action at this time.
0 commit comments