This function has crashed: "Error - Could not find Next.js server" for any non-existing page

Netlify site: wheresbaldo-dev.netlify.app
Framework: next.js, v13.4.2
Next.js Runtime: v4.41.1

Issue:
when trying to access any page that doesn’t exist, instead of a 404 error page like I get in my local dev environment, I get a “This function has crashed” modal with an error saying the next.js server couldn’t be found.

I’m not using any custom error pages in my next.js config, just the built-in out of the box next.js error pages and error handling.

The site otherwise seems to load fine.

I can’t debug in dev because the correct 404 page comes up when trying to access non-existing pages, so I’m at a loss as to what’s happening.

stack trace:

Error: Could not find Next.js server
    at Object.<anonymous> (/var/task/.netlify/functions-internal/___netlify-handler/___netlify-handler.js:2:9)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
    at Module.load (node:internal/modules/cjs/loader:1119:32)
    at Module._load (node:internal/modules/cjs/loader:960:12)
    at Module.require (node:internal/modules/cjs/loader:1143:19)
    at require (node:internal/modules/cjs/helpers:119:18)
    at Object.<anonymous> (/var/task/___netlify-handler.js:1:18)
    at Module._compile (node:internal/modules/cjs/loader:1256:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)

Any Netlify staff able to provide any insight?

This appears to be a Netlify error

This isn’t happening for anyone else, so it’s probably related to your setup. I can see you’re using yarn, can you see if this happens with npm too? This has been an issue with Next.js in the past: Cannot find module ‘next/dist/server/next-server.js’ · vercel/next.js · Discussion #39432 (github.com), so maybe if Next.js is not being installed correctly in the node_modules directory, maybe it’a failing to bundle correctly. I’d recommend checking if the module is getting installed correctly in the node_modules directory, but if not, fixing that in your setup would be the first step.

Appreciate the reply hrishikesh, but a big part of the issue here is that I need to redeploy any changes each time to see if it fixes the issue, because everything works fine locally. Even when I run the full production build on my end, the error pages are called without issue, so it’s only happening on the live site with Netlify.

The github issue chain you linked to seems to be primarily for people using monorepos due to hoisting issues, which wouldn’t be the case for me since I’m not using a monorepo. And switching to npm would mean adding in node_modules and all of its bloat, which I specifically want to avoid, and hence why I’m using Yarn Berry.

In any case, I’ll just keep looking into it when I have time, because I want to avoid using npm as much as possible. But thanks again for the response.

I believe you can run netlify serve command locally and that spins up a server instance for a site that’s already built, though I don’t know if it works for Next.js sites. If it does, you won’t have to re-deploy everytime.

If you are referring to the Netlify CLI, I installed it previously, and it totally messed with my app’s structure by creating a node_modules folder (which is what I don’t want as mentioned above), and broke my app.

I ended up having to uninstall the CLI and spend an hour or so trying figuring out how to fix my app afterwards because of all the changes it made to the folder structure.

Maybe it’s something I did, but I’m pretty sure I followed the install directions for the CLI, so I can’t imagine why it’s screwing around with folder structures.
But in the end it didn’t help and only made things worse for me.

Appreciate the idea though.