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

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"
  },
2 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