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

@hrishikesh some more info. I think that it’s all down to the __NEXT_PRIVATE_PREBUNDLED_REACT variable not being set, or not being able to set it, due to naming restrictions on Netlify and AWS.

I have managed to patch the netlify next-runtime package locally, and it’s all down to a single line of code:

The environment variable is not set, so it always gets the default mappings, and not the next or experimental mappings. I also had to add appDir: true into my next.config.js, even though it’s no longer classed as experimental in the latest NextJS release.

I have a similar patch to @lewisking , so it will work by creating a prebuild.js and executing it as a prebuild script in package.json.

console.log("********* PREBUILDING");
const path = require("node:path");
const fs = require("fs");
const baseDir = process.cwd();

// /node_modules/@netlify/plugin-nextjs/lib/templates/requireHooks.js

const prebuildScripts = async () => {
  const file = path.join(
    baseDir,
    "/node_modules",
    "@netlify/plugin-nextjs/lib/templates/requireHooks.js"
  );

  const content = await fs.promises.readFile(file, "utf-8");
  await fs.promises.writeFile(
    file,
    content.replace(
      "const reactMode = process.env.__NEXT_PRIVATE_PREBUNDLED_REACT || 'default';",
      "const reactMode = process.env.__NEXT_PRIVATE_PREBUNDLED_REACT || 'next';"
    )
  );
};

prebuildScripts();

This seems to have worked for me on Next 13.4.9, with the appDir experimental config still set to true.

I’ve updated this ticket [Bug]: ISR Working Locally But Not Working on Netify · Issue #2127 · netlify/next-runtime · GitHub, so hopefully some work can be done to resolve the issue :slight_smile:

But at least for now, I have a fix

Blockquote

I have the same exact error. I am not adding any env variables that might clash with AWS of andriasys. what should i do to resolve it.

Hi everyone,

I just chime in to see what’s the state of this issue. It’s still not fixed in next-runtime 4.39.1, right? Is it currently being worked on?

Thank you!

We’re facing this issue on 13.4.13 any updates? Hacking this flag does not work →

__NEXT_PRIVATE_PREBUNDLED_REACT

Check where you’re hacking it. This worked for me:

Hi @hrishkesh, I’m using "@netlify/plugin-nextjs": "^4.39.1" and "next": "^13.4.12". I’m getting an error when trying to deploy now:

Invalid AWS Lambda parameters used in this request.. With 4.30.4 the website deploys but I get the 500 error mentioned above.

Hi, can you please add the env variable to site ID 7c02dfbd-77cc-404c-934b-3cc79c501acf?

This issue is still being worked on.

Just to keep this thread active, I also have this problem. I am trying to migrate all the sites from my company to netlify and this errors are a pain in the ass.
We currently have a Linux Server with Dokku, and works just fine the exactly same projects.

1 Like

I also have this problem, I am receiving 500 internal server error on dynamic routes. Nest.js SSR function handler logs show the following error:

Package subpath ‘./server.edge’ is not defined by “exports” in /var/task/node_modules/react-dom/package.json

Is there an update on this issue? I’ve tried the env hack above with no luck.

2 Likes

No significant updates so far

1 Like

I have the same error, this happened when trying to hit draft mode as part of the next js app dir, which would be SSR rendered.

Tried adding the env but get the AWS error as well. I tried the prebuild as well but that didn’t work.

+1 facing this. Next.js 13.4.13, app directory, NEXT_DISABLE_NETLIFY_EDGE set true because it’s incompatible with my middleware.

I tried the following:

  1. Apply the rebuild here - Server.edge not defined Error on nextJS SSR functions cause site to return 500 Errors - #31 by lewisking. This changes the error to the one below.
  2. This prebuild here - Server.edge not defined Error on nextJS SSR functions cause site to return 500 Errors - #99 by nick.howard. This does nothing.

The new error becomes this:

Runtime.ImportModuleError - Error: Cannot find module 'next/dist/compiled/react' Require stack: - /var/task/node_modules/next/dist/server/require-hook.js - /var/task/node_modules/next/dist/server/next-server.js - /var/task/.netlify/functions-internal/___netlify-handler/___netlify-handler.js - /var/task/___netlify-handler.js - /var/runtime/index.mjs

Anything else to try? Can anyone who’s spent some time with this outline the different approaches to working around this? I scrolled the thread but it’s been ongoing since May, lots of new versions, so it’s not clear what might/should work with Next 13.4.13.

This is part of my exploration in moving to from a Node.js server on Render.com to Netlify. I’d love to get this resolved so I can make a fair comparison.

@clgnyc - did you also keep the appDir flag in your next.config.js file?

experimental: {
  appDir: true,
}

Although it’s not needed by Next, it is needed by Netlify.

You can see what’s needed by tracing through the code in this file:
node_modules/@netlify/plugin-nextjs/lib/templates/requireHooks.js

I fixed my issues using a combination of fixes from this and other threads. I did not test these steps in the order I’ll post them here, but I believe they all together fix the issue.

  1. Downgrade to “next”: “13.4.9”
  2. in next.config.js add this to your export,
experimental: {
  appDir: true,
}
  1. Follow the steps in Server.edge not defined Error on nextJS SSR functions cause site to return 500 Errors - #31 by lewisking

I cannot understate how much I hate when a product puts me through this kind of esoteric ritual, and it’s making me reconsider using netlify. Huge props to the community.

6 Likes

Thanks! This summary helped me fix my issues after watching updates on this thread and github for days. No solution worked for me until I added the appDir flag to next.config.js which I assumed wasn’t needed since Next 13.4.

To summarise my final config:

  • I’m using “next”: “13.4.7”
  • I used ‘patch-package’ to make the modification from this post using “@netlify/plugin-nextjs”: “^4.40.0”

This fixed my issue of my “not-found.tsx” and “error.tsx” pages throw exceptions.

1 Like

I did, yes. No change. I’m keeping an eye on this thread but I’m expecting to continue using Render.com for now.

Out of curiosity - are you using the app router or pages router? I’m getting this issue on my app router project.