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

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