How to Debug a Next.js Application in VS Code (Complete Guide)
Next.js has two separate debugging environments — server and client. Here's how to attach VS Code to both simultaneously, debug Server Components, API routes, hydration errors, and middleware with breakpoints that actually work.
Understanding the Two Environments
Next.js debugging is split across two environments: the server (Node.js process running your Server Components and API routes) and the client (browser running your Client Components). VS Code can attach to both simultaneously.
| Environment | What runs here | How to debug |
|---|---|---|
| Server | Server Components, API routes, middleware, getServerSideProps | VS Code Node.js debugger |
| Client | Client Components ('use client'), browser events, state | Browser DevTools + VS Code Chrome debugger |
Errors in Server Components appear in the terminal. Errors in Client Components appear in the browser console. Knowing which side you're on determines which tool to open.
Step 1: Configure VS Code for Next.js
Create .vscode/launch.json:
Use "Next.js: debug full stack" to attach VS Code to both server and browser simultaneously. Set breakpoints in Server Components and Client Components — both pause in VS Code.
Step 2: Debug Server Components
Server Components run on the Node.js server. Errors appear in the terminal where npm run dev is running.
Set a breakpoint on the fetchUserData() line. When the page loads, VS Code pauses and you can inspect data before it reaches the component.
Note: Server Components run on each request — breakpoints trigger every time the page is visited. Use conditional breakpoints (right-click → Edit Breakpoint → add condition) to prevent pausing on every load.
Step 3: Debug Client Components
Client Components ('use client') run in the browser. Use browser DevTools or VS Code Chrome debugger.
With "Next.js: debug full stack" running, breakpoints in Client Components pause in VS Code — not in the browser. Source maps translate browser execution back to your TypeScript source.
Tip: If VS Code breakpoints in Client Components don't bind, check that
sourceMapPathOverridesin your launch config maps webpack paths to your source. The default Next.js config usually works without extra setup.
Step 4: Debug API Routes
API routes (in app/api/ or pages/api/) run on the server. Same debugger as Server Components.
Test API routes with curl while the debugger is attached:
VS Code pauses at the breakpoint, you inspect body, step through createUser, see the return value.
Step 5: Fix Hydration Errors
The most common Next.js error:
Hydration mismatch happens when server-rendered HTML doesn't match what React renders on the client. Common causes:
Cause: browser-only API used in a component that renders on both sides
Fix: Gate browser-only code with
useEffect(runs client-side only).
Cause: date or random value rendered differently on server vs client
Fix: Move dynamic values into state initialized in
useEffect.
Step 6: Debug Middleware
Next.js middleware runs at the edge before the request reaches your route. Errors here are silent by default.
Note: Middleware runs in the Edge Runtime, not Node.js. VS Code's Node debugger can't attach to it. Use
console.logfor middleware debugging — logs appear in the terminal.
Common Next.js Errors Quick Reference
| Error | Environment | Cause |
|---|---|---|
| Hydration failed | Client | Server/client render mismatch |
window is not defined | Server | Browser API in Server Component |
useEffect not available | Server | Client hook in Server Component — add 'use client' |
| 404 on dynamic route | Server | generateStaticParams returned empty or dynamicParams = false |
| Middleware redirect loop | Edge | Redirect target also matches middleware — add exclusion |
| CORS on API route | Server | API route missing headers — add Access-Control-Allow-Origin response header |
For Next.js errors that span Server Components, Client Components, and API routes — especially data-fetching bugs where the wrong data arrives at the wrong component — paste the component tree and the error into DebugAI. It reads the full chain from server fetch to client render and identifies where data goes wrong.
Debug faster starting today.
Free VS Code extension. 10 sessions/day. No credit card.