diff --git a/packages/vite/src/node/server/middlewares/proxy.ts b/packages/vite/src/node/server/middlewares/proxy.ts index 6b282e0afea05b..bf658a9ea7474f 100644 --- a/packages/vite/src/node/server/middlewares/proxy.ts +++ b/packages/vite/src/node/server/middlewares/proxy.ts @@ -52,8 +52,17 @@ export function proxyMiddleware( proxy.on('error', (err, req, originalRes) => { // When it is ws proxy, res is net.Socket - const res = originalRes as http.ServerResponse | net.Socket - if ('req' in res) { + // originalRes can be falsy if the proxy itself errored + const res = originalRes as http.ServerResponse | net.Socket | undefined + if (!res) { + config.logger.error( + `${colors.red(`http proxy error: ${err.message}`)}\n${err.stack}`, + { + timestamp: true, + error: err, + }, + ) + } else if ('req' in res) { config.logger.error( `${colors.red(`http proxy error at ${originalRes.req.url}:`)}\n${ err.stack