Next.js Debugging Is Split in Two#
Next.js runs code in two places: the server (Node.js, for Server Components and API routes) and the browser (for Client Components). Errors in
each place look different, surface differently, and require different debugging approaches.
Before you debug anything, identify which side the error is on:
- Error in terminal where you ran
next dev → server-side
- Error in browser DevTools console → client-side
- Error during
next build → build-time (TypeScript, import errors, static generation)
This guide covers all three.
Debugging Server-Side Errors (Server Components + API Routes)#
Server Components run on the Node.js server. Their errors print to the terminal, not the browser.
Error: Error: ENOENT: no such file or directory, open '/app/.env.local'
This appears in terminal only. The browser shows a generic "An error occurred" page in production, or a red error overlay in dev mode.
Read the Terminal Stack Trace#
Next.js dev mode (next dev) prints the full stack trace for server errors:
Error: Cannot read properties of undefined (reading 'id')
at UserDashboard (./src/app/dashboard/page.jsx:23:18)
at async Page (./src/app/dashboard/page.jsx:10:3)
Start from the bottom of the trace, find your code (ignore Next.js internals), and go to that line.
Set Up VS Code Debugger for Next.js#
Create .vscode/launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Next.js: debug server",
"type": "node-terminal",
"request": "launch",
"command": "npm run dev"
}
]
}
Start debugging with F5. Set breakpoints in any Server Component, layout.jsx, or route.ts file. When a page request hits, the debugger pauses —
you can inspect server-side variables, auth state, and database query results directly.
▎ Note: In Next.js 14+ App Router, breakpoints work in both Server Components (server-side, in the terminal debugger) and Route Handlers. Client
▎ Components need the browser DevTools debugger, not the VS Code debugger.
Debugging Client-Side Errors (Client Components)
Client Components render in the browser. Their errors show in DevTools (F12 → Console).
The React Error Overlay
In dev mode, unhandled Client Component errors show a red overlay with the component stack:
TypeError: Cannot read properties of null (reading 'user')
The above error occurred in the <ProfileCard> component:
at ProfileCard (src/components/ProfileCard.jsx:12)
at div
at UserDashboard (src/app/dashboard/page.jsx:18)
Read the component stack bottom-up to find which component caused the error and which parent rendered it.
Use Chrome DevTools Sources Panel
For complex client-side bugs:
1. Open DevTools → Sources tab
2. Press Ctrl+P → type the component filename
3. Click a line number to set a breakpoint
4. Trigger the action that causes the error
5. Execution pauses — inspect variable state in the right panel
▎ Tip: Next.js dev mode generates source maps automatically. You can debug your original .jsx/.tsx files in DevTools — not the compiled output.
▎ Source maps make line numbers and variable names match your actual code.
Common Client-Side Errors
Error: Hydration failed because the server rendered HTML didn't match the client
Cause: Component renders differently on server vs client — usually new Date(), Math.random(), or window access during render. Fix: move to
useEffect. Full guide: How to Fix the Next.js Hydration Error (/blog/fix-nextjs-hydration-error).
Error: useState can only be used in a Client Component.
Add the "use client" directive at the top of the file.
▎ Fix: Add 'use client' as the very first line of the component file — above all imports.
Debugging API Routes
API routes (app/api