diff --git a/packages/server/src/listener.js b/packages/server/src/listener.js index d3d753c88a..04f13a442f 100644 --- a/packages/server/src/listener.js +++ b/packages/server/src/listener.js @@ -95,15 +95,16 @@ export default class Listener { // Use better error message if (addressInUse) { - error.message = `Address \`${this.host}:${this.port}\` is already in use.` - } + const address = this.socket || `${this.host}:${this.port}` + error.message = `Address \`${address}\` is already in use.` - // Listen to a random port on dev as a fallback - if (addressInUse && this.dev && this.port !== '0') { - consola.warn(error.message) - consola.info('Trying a random port...') - this.port = '0' - return this.close().then(() => this.listen()) + // Listen to a random port on dev as a fallback + if (this.dev && !this.socket && this.port !== '0') { + consola.warn(error.message) + consola.info('Trying a random port...') + this.port = '0' + return this.close().then(() => this.listen()) + } } // Throw error diff --git a/packages/server/test/listener.test.js b/packages/server/test/listener.test.js index 0747fe05fd..e53624fd6d 100644 --- a/packages/server/test/listener.test.js +++ b/packages/server/test/listener.test.js @@ -330,6 +330,15 @@ describe('server: listener', () => { expect(() => listener.serverErrorHandler(addressInUse)).toThrow('Address `localhost:3000` is already in use.') }) + test('should throw address in use error for socket', () => { + const listener = new Listener({}) + listener.socket = 'nuxt.socket' + + const addressInUse = new Error() + addressInUse.code = 'EADDRINUSE' + expect(() => listener.serverErrorHandler(addressInUse)).toThrow('Address `nuxt.socket` is already in use.') + }) + test('should fallback to a random port in address in use error', async () => { const listener = new Listener({ dev: true }) listener.host = 'localhost'