fix: externalize global-agent and exclude Node built-ins from webpack

This commit is contained in:
Bhagya Amarasinghe
2025-12-15 17:05:21 +05:30
parent 5ebf406552
commit adab35d828
2 changed files with 13 additions and 4 deletions
+9 -3
View File
@@ -12,7 +12,12 @@ export const setupGlobalAgentProxy = (): void => {
if (globalThis.window !== undefined) {
return;
}
if (globalThis.process === undefined || globalThis.process.release?.name !== "node") {
// Hard guard: only run in real Node.js runtime (not edge/serverless)
if (
globalThis.process === undefined ||
globalThis.process.release?.name !== "node" ||
globalThis.process.versions?.node === undefined
) {
return;
}
@@ -36,9 +41,10 @@ export const setupGlobalAgentProxy = (): void => {
}
try {
// Dynamic require keeps global-agent out of non-node bundles
// Dynamic require prevents bundling into edge/serverless builds
// Using string concatenation to prevent webpack from statically analyzing the require
// eslint-disable-next-line @typescript-eslint/no-var-requires, turbo/no-undeclared-env-vars
const { bootstrap } = require("global-agent");
const { bootstrap } = require("global" + "-agent");
bootstrap();
globalThis.__FORMBRICKS_GLOBAL_AGENT_INITIALIZED = true;
logger.info("Enabled global-agent proxy support for outbound HTTP requests");
+4 -1
View File
@@ -19,7 +19,7 @@ const nextConfig = {
output: "standalone",
poweredByHeader: false,
productionBrowserSourceMaps: true,
serverExternalPackages: ["@aws-sdk", "@opentelemetry/instrumentation", "pino", "pino-pretty"],
serverExternalPackages: ["@aws-sdk", "@opentelemetry/instrumentation", "pino", "pino-pretty", "global-agent"],
outputFileTracingIncludes: {
"/api/auth/**/*": ["../../node_modules/jose/**/*"],
},
@@ -113,6 +113,9 @@ const nextConfig = {
config.resolve.fallback = {
http: false, // Prevents Next.js from trying to bundle 'http'
https: false,
net: false, // Prevents Next.js from trying to bundle 'net' (used by global-agent)
tls: false, // Prevents Next.js from trying to bundle 'tls' (used by global-agent)
domain: false, // Prevents Next.js from trying to bundle 'domain' (used by global-agent dependencies)
};
return config;
},