Skip to content

Commit 652f20a

Browse files
wconrad265tlane25t
authored
feat: port in use (#6887)
* feat: more clear error messege nforms the user that this could be from a netlify function opening ports Co-authored-by: Thomas Lane <[email protected]> * fix: updated port in use error message Co-authored-by: Thomas Lane <[email protected]> * fix: added types for EADDRINUSE error and updated error message Co-authored-by: Will <[email protected]> * fix: reverted back to commit before prettier and pnpm were installed Co-authored-by: Thomas Lane <[email protected]> --------- Co-authored-by: Thomas Lane <[email protected]> Co-authored-by: Thomas Lane <[email protected]> Co-authored-by: t <[email protected]>
1 parent 33ae41c commit 652f20a

File tree

2 files changed

+43
-22
lines changed

2 files changed

+43
-22
lines changed

src/commands/main.ts

+35-22
Original file line numberDiff line numberDiff line change
@@ -37,30 +37,43 @@ import { createStatusCommand } from './status/index.js'
3737
import { createSwitchCommand } from './switch/index.js'
3838
import { createUnlinkCommand } from './unlink/index.js'
3939
import { createWatchCommand } from './watch/index.js'
40-
40+
import { AddressInUseError } from './types.js'
4141
const SUGGESTION_TIMEOUT = 1e4
4242

43-
process.on('uncaughtException', async (err) => {
44-
console.log('')
45-
error(
46-
`${chalk.red(
47-
'Netlify CLI has terminated unexpectedly',
48-
)}\nThis is a problem with the Netlify CLI, not with your application.\nIf you recently updated the CLI, consider reverting to an older version by running:\n\n${chalk.bold(
49-
'npm install -g netlify-cli@VERSION',
50-
)}\n\nYou can use any version from ${chalk.underline(
51-
'https://ntl.fyi/cli-versions',
52-
)}.\n\nPlease report this problem at ${chalk.underline(
53-
'https://ntl.fyi/cli-error',
54-
)} including the error details below.\n`,
55-
{ exit: false },
56-
)
57-
58-
const systemInfo = await getSystemInfo()
59-
60-
console.log(chalk.dim(err.stack || err))
61-
console.log(chalk.dim(systemInfo))
62-
63-
reportError(err, { severity: 'error' })
43+
process.on('uncaughtException', async (err: AddressInUseError | Error) => {
44+
if ('code' in err && err.code === 'EADDRINUSE') {
45+
error(
46+
`${chalk.red(`Port ${err.port} is already in use`)}\n\n` +
47+
`Your serverless functions might be initializing a server\n` +
48+
`to listen on specific port without properly closing it.\n\n` +
49+
`This behavior is generally not advised\n` +
50+
`To resolve this issue, try the following:\n` +
51+
`1. If you NEED your serverless function to listen on a specific port,\n` +
52+
`use a randomly assigned port as we do not officially support this.\n` +
53+
`2. Review your serverless functions for lingering server connections, close them\n` +
54+
`3. Check if any other applications are using port ${err.port}\n`,
55+
{ exit: false },
56+
)
57+
} else {
58+
error(
59+
`${chalk.red(
60+
'Netlify CLI has terminated unexpectedly',
61+
)}\nThis is a problem with the Netlify CLI, not with your application.\nIf you recently updated the CLI, consider reverting to an older version by running:\n\n${chalk.bold(
62+
'npm install -g netlify-cli@VERSION',
63+
)}\n\nYou can use any version from ${chalk.underline(
64+
'https://ntl.fyi/cli-versions',
65+
)}.\n\nPlease report this problem at ${chalk.underline(
66+
'https://ntl.fyi/cli-error',
67+
)} including the error details below.\n`,
68+
{ exit: false },
69+
)
70+
71+
const systemInfo = await getSystemInfo()
72+
73+
console.log(chalk.dim(err.stack || err))
74+
console.log(chalk.dim(systemInfo))
75+
reportError(err, { severity: 'error' })
76+
}
6477

6578
process.exit(1)
6679
})

src/commands/types.d.ts

+8
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,11 @@ export type NetlifyOptions = {
7373
state: StateConfig
7474
frameworksAPIPaths: FrameworksAPIPaths
7575
}
76+
77+
export interface AddressInUseError extends Error {
78+
code: 'EADDRINUSE'
79+
errno: number
80+
syscall: 'listen'
81+
address: string
82+
port: number
83+
}

0 commit comments

Comments
 (0)