Next 13 app is not running correctly

I am trying to deploy my Next.js (v13) app to Netlify, but getting al sorts of SSR errors.

I tried different combinations of next & @netlify/plugin-nextjs versions reported as ‘working’ by other users in posts like this one: https://answers.netlify.com/t/server-edge-not-defined-error-on-nextjs-ssr-functions-cause-site-to-return-500-errors

These attempts always result in 500 error on the page and this error in the SSR logs:

Oct 12, 09:41:26 PM: 53c5995b ERROR  Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './server.edge' is not defined by "exports" in /var/task/node_modules/react-dom/package.json
Oct 12, 09:41:26 PM: 53c5995b ERROR      at new NodeError (node:internal/errors:405:5)
Oct 12, 09:41:26 PM: 53c5995b ERROR      at exportsNotFound (node:internal/modules/esm/resolve:364:10)
Oct 12, 09:41:26 PM: 53c5995b ERROR      at packageExportsResolve (node:internal/modules/esm/resolve:700:9)
Oct 12, 09:41:26 PM: 53c5995b ERROR      at resolveExports (node:internal/modules/cjs/loader:567:36)
Oct 12, 09:41:26 PM: 53c5995b ERROR      at Module._findPath (node:internal/modules/cjs/loader:636:31)
Oct 12, 09:41:26 PM: 53c5995b ERROR      at Module._resolveFilename (node:internal/modules/cjs/loader:1063:27)
Oct 12, 09:41:26 PM: 53c5995b ERROR      at /var/task/.netlify/functions-internal/___netlify-handler/requireHooks.js:100:40
Oct 12, 09:41:26 PM: 53c5995b ERROR      at Module._load (node:internal/modules/cjs/loader:922:27)
Oct 12, 09:41:26 PM: 53c5995b ERROR      at Module.require (node:internal/modules/cjs/loader:1143:19)
Oct 12, 09:41:26 PM: 53c5995b ERROR      at require (node:internal/modules/cjs/helpers:121:18) {
Oct 12, 09:41:26 PM: 53c5995b ERROR    code: 'ERR_PACKAGE_PATH_NOT_EXPORTED'Oct 12, 09:41:26 PM: 53c5995b ERROR  }

Adding a prebuild script from this comment: Server.edge not defined Error on nextJS SSR functions cause site to return 500 Errors - #99 by nick.howard
Changes error on the page to ‘Internal Server Error’ and adds this error to the server.edge error in the logs:

Oct 12, 10:23:19 PM: 62d0534c ERROR  TypeError: Cannot read properties of null (reading 'useContext')
Oct 12, 10:23:19 PM: 62d0534c ERROR      at exports.useContext (/var/task/node_modules/next/dist/compiled/react/cjs/react.production.min.js:28:118)
Oct 12, 10:23:19 PM: 62d0534c ERROR      at Head (/var/task/.next/server/chunks/523.js:325:44)
Oct 12, 10:23:19 PM: 62d0534c ERROR      at Wc (/var/task/node_modules/react-dom/cjs/react-dom-server.browser.production.min.js:68:44)
Oct 12, 10:23:19 PM: 62d0534c ERROR      at Zc (/var/task/node_modules/react-dom/cjs/react-dom-server.browser.production.min.js:70:253)
Oct 12, 10:23:19 PM: 62d0534c ERROR      at Z (/var/task/node_modules/react-dom/cjs/react-dom-server.browser.production.min.js:76:89)
Oct 12, 10:23:19 PM: 62d0534c ERROR      at $c (/var/task/node_modules/react-dom/cjs/react-dom-server.browser.production.min.js:78:98)
Oct 12, 10:23:19 PM: 62d0534c ERROR      at bd (/var/task/node_modules/react-dom/cjs/react-dom-server.browser.production.min.js:77:404)
Oct 12, 10:23:19 PM: 62d0534c ERROR      at Z (/var/task/node_modules/react-dom/cjs/react-dom-server.browser.production.min.js:76:217)
Oct 12, 10:23:19 PM: 62d0534c ERROR      at $c (/var/task/node_modules/react-dom/cjs/react-dom-server.browser.production.min.js:78:98)
Oct 12, 10:23:19 PM: 62d0534c ERROR      at Zc (/var/task/node_modules/react-dom/cjs/react-dom-server.browser.production.min.js:71:145)
Oct 12, 10:23:19 PM: 62d0534c ERROR  TypeError: Cannot read properties of null (reading 'useContext')
Oct 12, 10:23:19 PM: 62d0534c ERROR      at exports.useContext (/var/task/node_modules/next/dist/compiled/react/cjs/react.production.min.js:28:118)
Oct 12, 10:23:19 PM: 62d0534c ERROR      at Head (/var/task/.next/server/chunks/523.js:325:44)
Oct 12, 10:23:19 PM: 62d0534c ERROR      at Wc (/var/task/node_modules/react-dom/cjs/react-dom-server.browser.production.min.js:68:44)
Oct 12, 10:23:19 PM: 62d0534c ERROR      at Zc (/var/task/node_modules/react-dom/cjs/react-dom-server.browser.production.min.js:70:253)
Oct 12, 10:23:19 PM: 62d0534c ERROR      at Z (/var/task/node_modules/react-dom/cjs/react-dom-server.browser.production.min.js:76:89)
Oct 12, 10:23:19 PM: 62d0534c ERROR      at $c (/var/task/node_modules/react-dom/cjs/react-dom-server.browser.production.min.js:78:98)
Oct 12, 10:23:19 PM: 62d0534c ERROR      at bd (/var/task/node_modules/react-dom/cjs/react-dom-server.browser.production.min.js:77:404)

My next.config.js:

/** @type {import('next').NextConfig} */
const nextConfig = {
    experimental: {
        appDir: true,
    },
};

module.exports = nextConfig;

package.json contents as of now:

  "scripts": {
    "dev": "next dev",
    "prebuild": "node prebuild.js",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "@coreui/coreui": "^5.0.0-alpha.2",
    "@coreui/icons": "^3.0.1",
    "@coreui/icons-react": "^2.2.1",
    "@coreui/react": "^5.0.0-alpha.0",
    "axios": "^1.5.0",
    "eslint": "8.48.0",
    "eslint-config-next": "13.4.19",
    "joi": "^17.9.2",
    "jsonwebtoken": "^9.0.1",
    "jwks-rsa": "^3.0.1",
    "next": "^13.4.19",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-hook-form": "^7.45.4",
    "styled-components": "^6.0.8",
    "swr": "^2.2.2",
    "typescript": "5.2.2",
    "uuid": "^9.0.0",
    "zustand": "^4.4.1"
  },
  "devDependencies": {
    "@netlify/plugin-nextjs": "^4.40.2",
    "@types/jwk-to-pem": "^2.0.1",
    "@types/node": "20.5.6",
    "@types/react": "^18.2.28",
    "@types/react-dom": "^18.2.13",
    "@types/styled-components": "^5.1.28",
    "@types/uuid": "^9.0.3"
  }

Site URL: https://hephaestus-merchant-stg.netlify.app/

How do I deploy the app correctly to Netlify?

Please do not open multiple threads about the same issue. You’ve already found the relevant thread and we’ve already said countless times in that, that we would notify folks when we have a solution.