chore(core): Stop server when access to port is not allowed (#17334)

This commit is contained in:
Andreas Fitzek
2025-07-22 11:39:26 +02:00
committed by GitHub
parent ee67e9e354
commit f2ca2df90c

View File

@@ -168,11 +168,24 @@ export abstract class AbstractServer {
this.server.on('error', (error: Error & { code: string }) => { this.server.on('error', (error: Error & { code: string }) => {
if (error.code === 'EADDRINUSE') { if (error.code === 'EADDRINUSE') {
// EADDRINUSE is thrown when the port is already in use
this.logger.info( this.logger.info(
`n8n's port ${port} is already in use. Do you have another instance of n8n running already?`, `n8n's port ${port} is already in use. Do you have another instance of n8n running already?`,
); );
process.exit(1); } else if (error.code === 'EACCES') {
// EACCES is thrown when the process is not allowed to use the port
// This can happen if the port is below 1024 and the process is not run as root
// or when the port is reserved by the system, for example Windows reserves random ports
// for NAT for Hyper-V and other virtualization software.
this.logger.info(
`n8n does not have permission to use port ${port}. Please run n8n with a different port.`,
);
} else {
// Other errors are unexpected and should be logged
this.logger.error('n8n webserver failed, exiting', { error });
} }
// we always exit on error, so that n8n does not run in an inconsistent state
process.exit(1);
}); });
await new Promise<void>((resolve) => this.server.listen(port, address, () => resolve())); await new Promise<void>((resolve) => this.server.listen(port, address, () => resolve()));