Troubleshooting Guide
Common issues and their solutions.
Table of Contents
- Build Issues
- Runtime Errors
- Performance Problems
- Component Issues
- API Issues
- Configuration Problems
- Deployment Issues
Build Issues
Build Fails with TypeScript Errors
Problem: TypeScript compilation errors during build.
Solutions:
-
Check TypeScript version compatibility
npm list typescript -
Clear build cache
rm -rf .next npm run build -
Check for type errors
npx tsc --noEmit -
Update TypeScript
npm install typescript@latest
Module Not Found Errors
Problem: Cannot find module errors.
Solutions:
-
Reinstall dependencies
rm -rf node_modules package-lock.json npm install -
Check import paths
- Use
@/alias for imports - Verify file paths are correct
- Use
-
Clear Next.js cache
rm -rf .next
Build Timeout
Problem: Build process times out.
Solutions:
-
Increase Node.js memory
NODE_OPTIONS="--max-old-space-size=4096" npm run build -
Check for large dependencies
npm run analyze -
Optimize bundle size
- Remove unused dependencies
- Use dynamic imports
Runtime Errors
Hydration Mismatch
Problem: React hydration errors.
Solutions:
-
Check for client-only code in server components
'use client'; // Component code -
Use dynamic imports for client-only components
import dynamic from 'next/dynamic'; const ClientComponent = dynamic(() => import('./ClientComponent'), { ssr: false, }); -
Ensure consistent rendering
- Check for Date/Time differences
- Verify random values
"window is not defined" Error
Problem: Using browser APIs in server components.
Solutions:
-
Check if running in browser
if (typeof window !== 'undefined') { // Browser code } -
Use useEffect for browser APIs
useEffect(() => { // Browser code }, []); -
Move to client component
'use client';
API Route Not Found
Problem: 404 errors for API routes.
Solutions:
-
Check route file location
- Should be in
app/api/[route]/route.ts
- Should be in
-
Verify export names
export async function GET() {} export async function POST() {} -
Check Next.js version
- Ensure using App Router format
Performance Problems
Slow Page Loads
Problem: Pages load slowly.
Solutions:
-
Check bundle size
npm run analyze -
Optimize images
- Use Next.js Image component
- Convert to WebP/AVIF
- Use appropriate sizes
-
Enable caching
const data = await advancedCache.get('key', fetcher, { strategy: 'cache-first', ttl: 60000, }); -
Lazy load components
import dynamic from 'next/dynamic'; const HeavyComponent = dynamic(() => import('./HeavyComponent'));
High Memory Usage
Problem: Application uses too much memory.
Solutions:
-
Check for memory leaks
- Use DevTools memory profiler
- Check for event listeners
-
Optimize state management
- Use local state when possible
- Clear unused state
-
Monitor memory
import { useMemory } from '@/hooks/useMemory'; const { memory } = useMemory();
Slow API Responses
Problem: API calls are slow.
Solutions:
-
Enable caching
const { data } = useDataFetch('/api/data', { cache: { strategy: 'cache-first', ttl: 60000 }, }); -
Optimize API routes
- Add response caching
- Optimize database queries
- Use connection pooling
-
Check network
- Verify API endpoint
- Check for rate limiting
Component Issues
Modal Not Closing
Problem: Modal doesn't close when expected.
Solutions:
-
Check
isOpenstateconst [isOpen, setIsOpen] = useState(false); <Modal isOpen={isOpen} onClose={() => setIsOpen(false)} /> -
Verify event handlers
- Ensure
onCloseis called - Check for event propagation
- Ensure
-
Check for multiple modals
- Ensure only one modal is open
Tooltip Not Showing
Problem: Tooltip doesn't appear.
Solutions:
-
Check z-index
- Ensure tooltip has high z-index
- Check for overlapping elements
-
Verify trigger element
- Ensure element is interactive
- Check for pointer-events
-
Check positioning
- Verify viewport space
- Check for overflow hidden
Form Validation Not Working
Problem: Form validation doesn't trigger.
Solutions:
-
Check validation rules
validation: { email: { required: true, email: true }, } -
Verify form submission
<form onSubmit={handleSubmit}> -
Check error display
{errors.email && <span>{errors.email}</span>}
API Issues
CORS Errors
Problem: Cross-origin request blocked.
Solutions:
-
Configure CORS in API route
export async function GET() { return NextResponse.json(data, { headers: { 'Access-Control-Allow-Origin': '*', }, }); } -
Use Next.js rewrites
// next.config.ts async rewrites() { return [ { source: '/api/:path*', destination: 'https://api.example.com/:path*', }, ]; }
Rate Limiting
Problem: Too many requests error.
Solutions:
-
Check rate limit configuration
const result = rateLimiter.check('user-id', 100, 60000); -
Implement retry logic
const { retry } = useRetry(fetchData, { maxRetries: 3, delay: 1000, }); -
Add request throttling
const throttledFetch = throttle(fetchData, 1000);
API Timeout
Problem: API requests timeout.
Solutions:
-
Increase timeout
const data = await fetchWithCache(url, { timeout: 30000, }); -
Add retry logic
const { data } = useDataFetch(url, { retries: 3, retryDelay: 1000, }); -
Check API endpoint
- Verify endpoint is accessible
- Check network connectivity
Configuration Problems
Environment Variables Not Working
Problem: Environment variables not accessible.
Solutions:
-
Check variable naming
- Must start with
NEXT_PUBLIC_for client - Restart dev server after changes
- Must start with
-
Verify file location
- Should be
.env.localin root - Check
.gitignore
- Should be
-
Access in code
process.env.NEXT_PUBLIC_SITE_URL
Feature Flags Not Working
Problem: Feature flags don't toggle.
Solutions:
-
Check configuration
export const featureFlags = { myFeature: process.env.NEXT_PUBLIC_MY_FEATURE === 'true', }; -
Verify hook usage
const enabled = useFeatureFlag('myFeature'); -
Check environment variable
- Ensure variable is set
- Restart server
i18n Not Working
Problem: Translations not showing.
Solutions:
-
Check locale setting
i18n.setLocale('de'); -
Verify translations exist
i18n.addTranslations('de', { common: { loading: 'Lädt...' }, }); -
Check hook usage
const { t } = useI18n(); const text = t('common.loading');
Deployment Issues
Build Fails on Deployment
Problem: Build fails in CI/CD.
Solutions:
-
Check Node.js version
- Ensure version 18+
- Set in CI/CD config
-
Verify environment variables
- Set in deployment platform
- Check variable names
-
Check build logs
- Review error messages
- Check for missing dependencies
Assets Not Loading
Problem: Static assets return 404.
Solutions:
-
Check file paths
- Use
/for public files - Verify file exists
- Use
-
Check Next.js config
// next.config.ts assetPrefix: process.env.NODE_ENV === 'production' ? '/prefix' : '', -
Verify build output
- Check
.next/static - Verify asset paths
- Check
PWA Not Working
Problem: PWA features not working.
Solutions:
-
Check manifest
- Verify
public/manifest.json - Check file paths
- Verify
-
Verify Service Worker
- Check
public/sw.js - Verify registration
- Check
-
Check HTTPS
- PWA requires HTTPS
- Verify SSL certificate
Getting More Help
Still Having Issues?
-
Check Documentation
- Review relevant guides
- Check API documentation
-
Search Issues
- Check GitHub issues
- Search for similar problems
-
Create Issue
- Provide error details
- Include reproduction steps
- Add environment info
Useful Commands
# Clear all caches
rm -rf .next node_modules package-lock.json
npm install
# Check for errors
npm run lint
npx tsc --noEmit
# Analyze bundle
npm run analyze
# Test build
npm run build
Debug Mode
Enable debug logging:
// In development
if (process.env.NODE_ENV === 'development') {
console.log('Debug info');
}
Common Error Messages
"Cannot read property of undefined"
Solution: Add null checks
if (data && data.property) {
// Use data.property
}
"Maximum update depth exceeded"
Solution: Check for infinite loops in useEffect
useEffect(() => {
// Ensure dependencies are correct
}, [dependency]);
"Text content does not match"
Solution: Check for hydration mismatches
- Ensure server and client render same content
- Use
suppressHydrationWarningif needed