Next auth api route throwing Error: Failed to collect page data for /api/auth/[...nextauth] only on production

Hello if you have some prisma logic inside your app, do not forget to build your app with prisma generate in the build command, I’ve had some Failed collect page data too and I resolved it by building my app with prisma generate (maybe its a whole other problem but dont forget to add prisma generate if you’ve forgot to do so)

1 Like

Hi there,

The error indicates that the issue is likely with your environment variables or server-side execution in production. Here are some steps to resolve it:

  1. Check Environment Variables:

    • Your NEXTAUTH_URL in production should point to your Vercel deployment URL, not http://localhost:3000. Update it in your Vercel environment settings:
      NEXTAUTH_URL=https://your-vercel-app-url.vercel.app
      
  2. Validate Prisma Configuration:

    • Ensure your DATABASE_URL is correct and accessible from the production environment. Test the connection locally to rule out any database-related issues.
  3. Build-Specific Errors:

    • Run npm run build locally to confirm there are no errors in the build process.
  4. Update Dependencies:

    • Ensure you’re using the latest versions of next-auth, @next-auth/prisma-adapter, and bcrypt.
  5. Debugging in Vercel:

    • Add detailed logging in your API route (api/auth/[...nextauth]/route.ts) to identify what’s failing. For example:
      console.log('Environment:', process.env);
      console.log('AuthOptions:', authOptions);
      
  6. Secret Key Issue:

    • Verify NEXTAUTH_SECRET is the same in both .env and Vercel.