Skip to content

Commit

Permalink
Allow port 0 in next dev and next start
Browse files Browse the repository at this point in the history
Co-authored-by: Steven <steven@ceriously.com>
  • Loading branch information
wbinnssmith and styfle committed Aug 31, 2022
1 parent dddc60d commit d9b131d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
9 changes: 6 additions & 3 deletions packages/next/cli/next-dev.ts
Expand Up @@ -75,9 +75,12 @@ const nextDev: cliCommand = (argv) => {
)
}
}
const allowRetry = !args['--port']
let port: number =
args['--port'] || (process.env.PORT && parseInt(process.env.PORT)) || 3000

const hasPortFlag = Object.hasOwnProperty.call(args, '--port')
const allowRetry = !hasPortFlag
let port: number = hasPortFlag
? args['--port']
: (process.env.PORT && parseInt(process.env.PORT)) || 3000

// we allow the server to use a random port while testing
// instead of attempting to find a random port and then hope
Expand Down
6 changes: 4 additions & 2 deletions packages/next/cli/next-start.ts
Expand Up @@ -52,8 +52,10 @@ const nextStart: cliCommand = (argv) => {
}

const dir = getProjectDir(args._[0])
let port: number =
args['--port'] || (process.env.PORT && parseInt(process.env.PORT)) || 3000
let port: number = Object.hasOwnProperty.call(args, '--port')
? args['--port']
: (process.env.PORT && parseInt(process.env.PORT)) || 3000

const host = args['--hostname'] || '0.0.0.0'

if (process.env.__NEXT_FORCED_PORT) {
Expand Down
13 changes: 13 additions & 0 deletions test/integration/cli/test/index.test.js
Expand Up @@ -178,6 +178,19 @@ describe('CLI Usage', () => {
expect(output).toMatch(new RegExp(`http://localhost:${port}`))
})

test('--port 0', async () => {
const output = await runNextCommandDev([dir, '--port', '0'], true)
const matches = /on 0.0.0.0:(\d+)/.exec(output)
expect(matches).not.toBe(null)

const port = parseInt(matches[1])
// Regression test: port 0 was interpreted as if no port had been
// provided, falling back to 3000.
expect(port).not.toBe(3000)

expect(output).toMatch(new RegExp(`http://localhost:${port}`))
})

test("NODE_OPTIONS='--inspect'", async () => {
// this test checks that --inspect works by launching a single debugger for the main Next.js process,
// not for its subprocesses
Expand Down

0 comments on commit d9b131d

Please sign in to comment.