diff --git a/apps/backend/package.json b/apps/backend/package.json index 4b8c467eba..731abb94bd 100644 --- a/apps/backend/package.json +++ b/apps/backend/package.json @@ -14,7 +14,7 @@ "dev": "BACKEND_PORT=${STACK_DEV_FALLBACK_BACKEND:+${NEXT_PUBLIC_STACK_PORT_PREFIX:-81}10} && BACKEND_PORT=${BACKEND_PORT:-${NEXT_PUBLIC_STACK_PORT_PREFIX:-81}02} && concurrently -n \"dev,codegen,prisma-studio,email-queue,cron-jobs,bulldozer-studio\" -k \"STACK_DISABLE_REACT_ASYNC_DEBUG_INFO=${STACK_DISABLE_REACT_ASYNC_DEBUG_INFO:-true} next dev --port $BACKEND_PORT ${STACK_BACKEND_DEV_EXTRA_ARGS:-}\" \"pnpm run codegen:watch\" \"pnpm run prisma-studio\" \"pnpm run run-email-queue\" \"pnpm run run-cron-jobs\" \"pnpm run run-bulldozer-studio\"", "dev:inspect": "STACK_BACKEND_DEV_EXTRA_ARGS=\"--inspect\" pnpm run dev", "dev:profile": "STACK_BACKEND_DEV_EXTRA_ARGS=\"--experimental-cpu-prof\" pnpm run dev", - "build": "pnpm run codegen && next build", + "build": "pnpm run codegen && next build && node scripts/patch-required-server-files.mjs", "docker-build": "pnpm run codegen && next build --experimental-build-mode compile", "build-self-host-migration-script": "tsdown --config scripts/db-migrations.tsdown.config.ts", "analyze-bundle": "next experimental-analyze", diff --git a/apps/backend/scripts/patch-required-server-files.mjs b/apps/backend/scripts/patch-required-server-files.mjs new file mode 100644 index 0000000000..cb9aec42ab --- /dev/null +++ b/apps/backend/scripts/patch-required-server-files.mjs @@ -0,0 +1,36 @@ +/** + * Workaround for https://github.com/vercel/next.js/issues/91661 + * + * Next.js 16.2 doesn't include `.next/package.json` (the CJS boundary marker) + * in the `required-server-files.json` manifest. Without it, Vercel's serverless + * functions inherit `"type": "module"` from the project's package.json, causing + * `require is not defined` at runtime. + * + * The upstream fix landed in vercel/next.js#93612 (canary only as of 16.2.6). + * This script patches the manifest after `next build` to include the file. + * + * Remove this script once we upgrade to a stable Next.js release containing the fix. + */ + +import { readFileSync, writeFileSync, existsSync } from 'fs'; +import { join } from 'path'; + +const distDir = '.next'; +const manifestPath = join(distDir, 'required-server-files.json'); + +if (!existsSync(manifestPath)) { + // Not all build modes produce this manifest (e.g. compile-only docker builds) + process.exit(0); +} + +const manifest = JSON.parse(readFileSync(manifestPath, 'utf8')); + +const packageJsonEntry = join(distDir, 'package.json'); + +if (!manifest.files.includes(packageJsonEntry)) { + manifest.files.push(packageJsonEntry); + writeFileSync(manifestPath, JSON.stringify(manifest)); + console.log(`Patched ${manifestPath}: added ${packageJsonEntry} to files array`); +} else { + console.log(`${manifestPath} already includes ${packageJsonEntry}, no patch needed`); +}