How to fix Next.js build errors and failed deployments

Your site runs fine with npm run dev, then the server logs turn red. This is very common: dev mode is far more forgiving than next build, and a Linux server differs from your Windows or macOS machine. The good news is that Next.js build errors fall into a handful of groups.

How to read a build log
The most important rule: find the first error, not the last line. Lines such as npm ERR! code ELIFECYCLE or Build failed only tell you it failed. The real cause is higher up, usually the first line with Error or Failed to compile.
Before pushing, reproduce it locally. Most build errors appear right away:
rm -rf .next
npm ci
npm run build
npm ci installs exactly what the lockfile says, which is closer to what a server does.
Group 1: Module not found
Module not found: Can't resolve './components/Header'
If the path starts with ./, ../ or @/, it is almost certainly a case mismatch. Windows and macOS treat Header.tsx and header.tsx as the same file, and Linux does not. Make the import match the file name.
Git on Windows sometimes misses case-only renames, so rename with:
git mv components/header.tsx components/Header.tsx
If it is a package name, such as Can't resolve 'framer-motion', the package is not in package.json. Run npm install framer-motion and commit both package.json and the lockfile.
Group 2: Type errors and ESLint
Type error: Property 'name' does not exist on type 'User'.
next dev does not stop on TypeScript errors, but next build does, which is why AI-written code often fails here. Paste the error into your AI tool for a targeted fix. You can bypass it with typescript.ignoreBuildErrors in next.config, but only when you must, since type errors often predict real bugs.
Group 3: Next.js specific errors
useSearchParams() should be wrapped in a suspense boundary: wrap the component using that hook in <Suspense>.
window is not defined or document is not defined: code running on the server is touching a browser object. Move it into useEffect, or load the component with dynamic(() => import(...), { ssr: false }) from a Client Component.
You're importing a component that needs useState: add "use client" at the top of the file.
ECONNREFUSED during the build: a page calls localhost or a database while building, when nothing is running. Call the data function directly, or mark the page as dynamic.
Group 4: Missing environment variables
Error: DATABASE_URL is not set
Declare the name in .env.example and set the value in your site's settings. Two notes for Next.js: NEXT_PUBLIC_ variables are embedded at build time, so changing one means rebuilding, and module-level code that reads a variable can break the whole build if the value is missing.
Group 5: Database errors
| Message | Meaning |
|---|---|
P1001: Can't reach database server | Wrong address, or the database is not ready |
password authentication failed | Wrong password in the connection string |
relation "users" does not exist | Migrations have not run |
ECONNREFUSED ...:5432 | Pointing at localhost instead of the real database |
Prisma projects need prisma generate before the build, usually via "postinstall": "prisma generate", and prisma migrate deploy to create the tables.
Group 6: Out of memory
FATAL ERROR: JavaScript heap out of memory
Also exit code 137 or OOMKilled. Next.js builds use a lot of RAM, so remove heavy unused libraries or give the site more memory. See the RAM notes in Next.js hosting.
The build passes but the site does not run
- Wrong port: the app listens on a different port from the configuration. Make sure a Dockerfile's
EXPOSEmatches. - Listening on
localhostonly: it must listen on0.0.0.0, see Node.js hosting.
How VibeVN helps
On VibeVN, the logs are read automatically to place the failure in one of these groups, quote the lines that prove it, and explain the fix. From the Basic plan, AI proposes a fix as a GitHub pull request for you to review. Not everything can be fixed automatically, and in those cases you still get the information needed to solve it.
New to this? Start with how to deploy a vibe coded website.
Frequently asked questions
Why does npm run dev work when the build fails?
Dev mode compiles only the page you open and does not stop on type errors. The build checks and optimizes everything.
It builds locally but fails on the server. Why?
Compare the Node.js version, upper and lower case in imports, and environment variables, and make sure your lockfile is committed.


