Next.js Fix, Crash & Optimization Guide
Troubleshoot Next.js build errors, hydration mismatches, and dev server crashes, plus optimization tips for App Router and Pages Router projects.
Next.js Fix, Crash & Optimization Guide
Next.js is a React framework for production web apps, giving you file-based routing, server-side rendering, and an integrated build pipeline. It is used by teams shipping both content sites and full-stack applications that need SSR, SSG, or API routes in one codebase.
Install / First Setup
Install the latest via the official scaffolder:
npx create-next-app@latest my-app
cd my-app
npm run dev
Next.js requires Node.js 18.18 or a newer LTS release. If you manage multiple Node versions, use nvm install --lts and nvm use. To disable anonymous telemetry during development: npx next telemetry disable.
Common Issues & Fixes
Hydration mismatch error
Cause: The server-rendered HTML differs from the first client render — common with Date.now(), Math.random(), or reading localStorage directly in render.
Fix: Move browser-only logic into useEffect, or load the component with next/dynamic using { ssr: false }. For a single attribute mismatch, suppressHydrationWarning on the element is a targeted escape hatch.
Port 3000 already in use
Cause: Another next dev process or a different app is bound to the default port.
Fix: npx next dev -p 3001, or set the port via the PORT environment variable.
Unsupported Node.js version
Cause: Running an End-of-Life or too-old Node release that Next.js no longer supports.
Fix: Install a current LTS (Node 18.18+/20/22) via nvm and re-run npm run dev.
Stale .next cache after upgrade
Cause: Cached build output conflicts with a new Next.js or React version.
Fix: Remove generated folders and reinstall: rm -rf .next node_modules package-lock.json && npm install.
"Export encountered errors" during build
Cause: Using the deprecated next export command, or a page that throws during static generation.
Fix: For static output use output: 'export' in next.config.js, or guard data fetching with dynamicParams/generateStaticParams. Inspect the per-page error message in the build log.
Performance & Optimization
- Low-End (4–8 GB RAM): Run
next devwith a capped heap:NODE_OPTIONS=--max-old-space-size=2048 npm run dev. Disable telemetry and avoid running heavy editors/DBs alongside. Use Turbopack dev (next dev --turboin Next 15+) for faster recompiles. - Mid (16 GB): Keep default caching; rely on SWC (the default compiler) and React Server Components to trim client JS. Run
next buildnormally. - Workstation (32 GB+): Increase heap for large monorepos:
NODE_OPTIONS=--max-old-space-size=8192. Parallelize CI builds and reuse the.next/cachebetween runs via remote caching (next build --experimental-debugfor profiling).
Reduce bundle size with route-level code splitting (already automatic via the App Router), next/dynamic for heavy client components, and by auditing with @next/bundle-analyzer.
Version & Compatibility Notes
- App Router (Next 13.4+) coexists with the older Pages Router; both are supported in the same app. App Router uses React Server Components by default.
- Next.js 15 stabilized Turbopack for development and requires React 19 for some features; React 18 is still supported.
- Node.js 18.18+ is the minimum supported runtime for current releases. Consult official release notes for the exact minimum per version.
FAQ
Q: Why do I get a hydration error only in production?
A: Production renders real HTML on the server while dev often shows the client output; differences from Date, Math.random(), or localStorage in render surface only when SSR output is actually served. Move that logic into useEffect or a client-only dynamic import.
Q: Should I use App Router or Pages Router for a new project? A: For new projects, the App Router is the recommended default. Pages Router remains fully supported for existing code and incremental migration.
Q: How do I fix "Cannot find module 'react'"?
A: Delete node_modules and package-lock.json, then npm install. Also confirm your node version meets the minimum requirement.
Q: Why is my production build failing on a page that works in dev?
A: Static generation runs the page at build time. Guard data access with generateStaticParams/dynamicParams, and handle missing data instead of throwing.
Q: How do I reduce dev memory usage?
A: Cap the heap with NODE_OPTIONS=--max-old-space-size=2048 and disable telemetry. Close other memory-heavy processes.
Q: Is next export still available?
A: It is deprecated. Use output: 'export' in next.config.js for static exports.
Related Guides
Accuracy Note
Commands and paths reflect common, real-world setups as of 2026-08. Always verify against your installed version and OS. When in doubt, consult the official Next.js documentation.
Calculator Recommended Adjustment Params
Run the Build Time Calculator with the values referenced in this guide to validate your rig before and after the fix.