Server.edge not defined Error on nextJS SSR functions cause site to return 500 Errors

Hi, I have found quite a janky solutions but works for me.

Turns out the __NEXT_PRIVATE_PREBUNDLED_REACT environment variable might have nothing to do with build process regarding to NextJS code, it uses to override to uses Vercel’s precompiled react-dom that has ./server.edge imports on next/server runtime. Means that this variable has to be present in Lambda functions. But because of some quirks you cannot add them directly.

So, I do some patches to make react overriding to always trigger before starts any builds and my site now works normally without issues.

cleanshot_2023-05-25_at_02.51.03

1 Like

Hi @frederikfink this isn’t necessary and won’t help. Also this does not need to be done on our end.

1 Like

Can confirm that applying the monkey patch above does work for me (next 13.4.3)

Where do you put this code?

In theory, anywhere before next-runtime bundling functions. But safely, I prefer to patch this code before running next build or any next cli.

This is how I did it:

  1. Create a prebuild script at prebuild.js in the route of your project. This is the code you want:
console.log("********* PREBUILDING");
const path = require("node:path");
const fs = require("fs");
const baseDir = process.cwd();

const prebuildScripts = async () => {
  const file = path.join(
    baseDir,
    "/node_modules",
    "next/dist/server/require-hook.js"
  );

  const content = await fs.promises.readFile(file, "utf-8");
  await fs.promises.writeFile(
    file,
    content.replace(
      "if (process.env.__NEXT_PRIVATE_PREBUNDLED_REACT) {",
      "if (true) {"
    )
  );
};

prebuildScripts();

  1. Update package.json to include a prebuild step:
  "scripts": {
    "dev": "next dev",
    "prebuild": "node prebuild.js",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
4 Likes

Thanks for sharing this with the community!

Yup this was the fix right here , i tried all other solutions from different posts but none of them worked

I’ve tried it but still not working, non-working route: brc-20.netlify.com/mint/ticker

How do you run this when your site builds on netlify?

For those trying to add the patch into the code/ asking how that will run during builds. Besides manually using the fs library to rewrite the code, you can also use patch-package or the patch option from pnpm

patch-package: GitHub - ds300/patch-package: Fix broken node modules instantly 🏃🏽‍♀️💨
pnpm patch: pnpm patch <pkg> | pnpm

Both are known as “patches” or “monkey patches”, I’m using the pnpm option.

pnpm will generate the changes below once you finish setting up what you want to change. That will be the code used by pnpm on each install (which will also happen on the netlify ci) making it work

Did you ever resolve this? Exact same issue still today.

Worked for me too! Thanks a lot!

I think Netlify runs prebuild script automatically, so just those 2 steps solved the problem

Wow it works on my website now ! Thank you @lewisking for this awesome script !!!

1 Like

So I created the prebuild.js file and pasted the code in. It’s in my root directory. added the npm script. but I’m still getting a failed build with the error: Invalid AWS Lambda parameters used in this request.

I’m using a NextJS 13 and the App Router and NextAuth. I made the move over to Vercel to continue testing, but they have their own set of issues and I’d really love to get this working on Netlify.

Additional errors from the Deploying phase:

2:54:34 PM: Starting to deploy site from ‘.next’

2:54:34 PM: Calculating files to upload

2:54:34 PM: 6 new files to upload

2:54:34 PM: 3 new functions to upload

2:55:56 PM: Failed to upload file: _ipx

2:55:56 PM: Failed to upload file: ___netlify-odb-handler

2:56:32 PM: Failed to upload file: ___netlify-handler

2:56:32 PM: Section completed: deploying

2:56:32 PM: Site deploy was successfully initiated

2:56:32 PM: ​

2:56:32 PM: (Deploy site completed in 1m 57.9s)

The fix for this has been released in Next.js Runtime v4.38.0. It would be soon rolled out to users using the plugin from the UI. But if someone wants to try it out now, feel free to install it manually: netlify/next-runtime: The Next.js Runtime allows Next.js to run on Netlify with zero configuration (github.com)

No other workaround should be needed.

The complete changelog can be found here: Release plugin-nextjs: v4.38.0 · netlify/next-runtime (github.com)

Using next-runtime 4.38.0, does not resolve the Issue on my side.
The prebuild script does work though.

Still error 500?
have you tried Error 500 nextjs 13 dinamic routing - #5 by designtoy ?
It’s a downgrade of next and the plugin. It worked out to me.

2 Likes

thanks for sharing this! (:

Yess with next-runtime 4.38.0 it still throws an 500 error.
But the error is thrown because of the missing export respectively the not prebuild react, which should be fixed with next-runtime 4.38.0 as stated in the release notes.

The error you are mentioning was happening to me before.
My app was only working with next < 13.3.2 and every upgrade to a higher version would result in a 500 error. But because of a different script error ( The app was working fine locally and on vercel ).

With the latest version of nextjs the error changed to the missing export though and i was able to resolve it with the prebuild script here.

But as said, only the prebuild script works, using next-runtime 4.38.0 does not resolve the issue.

EDIT: Nope actually it does not work again.
Now i got a 404 Error and the url gets a “/” appended.

Something is definitely broken. The only solution at the moment is the one mentioned by designtoy, from the other thread:

Downgrade nextjs to 13.3.1 and to use nextjs-runtime 4.36.1