From 5e7a2de12ba6fcddf019b739011d4834be4eeff6 Mon Sep 17 00:00:00 2001 From: bram-atmire Date: Sun, 28 Dec 2025 19:56:27 +0100 Subject: [PATCH 1/3] Fix PM2 cluster mode ECONNREFUSED errors MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Node.js 17+ changed DNS resolution to prefer IPv6 over IPv4. When running in PM2 cluster mode, this causes "ECONNREFUSED ::1:8080" errors if the backend only listens on IPv4 (127.0.0.1). This fix sets dns.setDefaultResultOrder('ipv4first') at the very start of the SSR bootstrap to ensure localhost resolves to IPv4. See: https://github.com/nodejs/node/issues/40537 🤖 Generated with [Claude Code](https://claude.ai/claude-code) Co-Authored-By: Claude --- src/main.server.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main.server.ts b/src/main.server.ts index be4bd8edf70..bb75303daf9 100644 --- a/src/main.server.ts +++ b/src/main.server.ts @@ -1,3 +1,12 @@ +/** + * Fix for Node.js 17+ where DNS resolution prefers IPv6 over IPv4. + * This causes "ECONNREFUSED ::1:8080" errors in PM2 cluster mode when + * the backend only listens on IPv4. + * See: https://github.com/nodejs/node/issues/40537 + */ +import * as dns from 'node:dns'; +dns.setDefaultResultOrder('ipv4first'); + import 'core-js/es/reflect'; import 'zone.js'; import 'reflect-metadata'; From 24c96de514fda33ab9bcdcba0cf5c329c85d8ad3 Mon Sep 17 00:00:00 2001 From: bram-atmire Date: Mon, 29 Dec 2025 21:45:29 +0100 Subject: [PATCH 2/3] Fix ESLint errors in main.server.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Change from namespace import to named import (import/no-namespace) - Move DNS configuration after all imports (import/first) - Fix import ordering (simple-import-sort/imports) All linting errors resolved. No functional changes to the DNS fix. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 --- src/main.server.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main.server.ts b/src/main.server.ts index bb75303daf9..81b96e08a15 100644 --- a/src/main.server.ts +++ b/src/main.server.ts @@ -4,9 +4,6 @@ * the backend only listens on IPv4. * See: https://github.com/nodejs/node/issues/40537 */ -import * as dns from 'node:dns'; -dns.setDefaultResultOrder('ipv4first'); - import 'core-js/es/reflect'; import 'zone.js'; import 'reflect-metadata'; @@ -16,6 +13,8 @@ import 'reflect-metadata'; */ import '@angular/localize/init'; +import { setDefaultResultOrder } from 'node:dns'; + import { bootstrapApplication, BootstrapContext, @@ -24,6 +23,9 @@ import { import { AppComponent } from './app/app.component'; import { serverAppConfig } from './modules/app/server-app.config'; +// Apply DNS resolution order fix for Node.js 17+ +setDefaultResultOrder('ipv4first'); + const bootstrap = (context: BootstrapContext) => bootstrapApplication(AppComponent, serverAppConfig, context); export default bootstrap; From 126a0a0fb9b95094f1937a74a626a5578f539d46 Mon Sep 17 00:00:00 2001 From: bram-atmire Date: Tue, 6 Jan 2026 16:41:17 +0100 Subject: [PATCH 3/3] Move DNS fix comment inline next to setDefaultResultOrder call MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Address PR feedback: remove file header comment and place the explanatory comment directly next to the code it documents. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- src/main.server.ts | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/main.server.ts b/src/main.server.ts index 81b96e08a15..6a4d7a2fa5d 100644 --- a/src/main.server.ts +++ b/src/main.server.ts @@ -1,9 +1,3 @@ -/** - * Fix for Node.js 17+ where DNS resolution prefers IPv6 over IPv4. - * This causes "ECONNREFUSED ::1:8080" errors in PM2 cluster mode when - * the backend only listens on IPv4. - * See: https://github.com/nodejs/node/issues/40537 - */ import 'core-js/es/reflect'; import 'zone.js'; import 'reflect-metadata'; @@ -23,7 +17,10 @@ import { import { AppComponent } from './app/app.component'; import { serverAppConfig } from './modules/app/server-app.config'; -// Apply DNS resolution order fix for Node.js 17+ +// Apply DNS resolution order fix for Node.js 17+ by preferring IPv4 over IPv6. +// This fixes "ECONNREFUSED ::1:8080" errors in PM2 cluster mode when +// the backend only listens on IPv4 +// See https://github.com/DSpace/dspace-angular/issues/4960 setDefaultResultOrder('ipv4first'); const bootstrap = (context: BootstrapContext) => bootstrapApplication(AppComponent, serverAppConfig, context);