| name | js-debugging |
| description | This skill should be used when the user asks to 'debug a JavaScript error', 'fix a TypeScript error', 'debug a React component', 'fix a Next.js error', 'debug an API route', 'fix a hydration error', 'investigate a runtime error', or mentions 'JS debugging', 'stack trace', 'React error boundary', 'hydration mismatch', 'unhandled rejection', 'type error'. Provides systematic JavaScript/TypeScript debugging methodology for Next.js applications covering runtime errors, type errors, React issues, hydration mismatches, and API failures. |
| license | MIT |
| metadata | {"author":"Chris Kelley (hello@iwritecode.io)","version":"1.0.0"} |
JavaScript/TypeScript Debugging Skill for Next.js Applications
Systematic debugging methodology for JavaScript, TypeScript, and Next.js applications. Follows four phases: root cause investigation, pattern analysis, hypothesis testing, and implementation.
Critical Rules
- No fixes without root cause. Never apply a fix unless you understand WHY the error occurs. Guessing wastes time and introduces new bugs.
- Read error messages completely. The full stack trace, including the "Caused by" chain and any nested errors. Do not stop at the first line.
- Check BOTH browser console AND server logs. Next.js runs code on both sides. A client error may originate server-side and vice versa.
- One change at a time. Make a single change, verify the result, then proceed. Batching changes makes it impossible to know what worked.
- Always create a failing test before fixing. If you cannot reproduce it in a test, you do not understand it well enough to fix it.
Phase 1: Root Cause Investigation (JS/TS-Specific)
Reading JavaScript Stack Traces
Stack traces are read top-to-bottom. The top frame is where the error was thrown; the bottom is where execution began.
TypeError: Cannot read properties of undefined (reading 'map')
at UserList (src/components/UserList.tsx:14:23) ← Error thrown here
at renderWithHooks (react-dom.development.js:149)
at mountIndeterminateComponent (react-dom.development.js:258)
at beginWork (react-dom.development.js:312)
Key steps:
- Look at the first frame that references YOUR code (not library code).
- Check the file, line number, and column number. With source maps enabled, these map to your original TypeScript.
- For async stack traces, look for
async frames and the ... X more frames sections — these often contain the real origin.
Checking Browser DevTools Console
Open DevTools (Cmd+Option+I) and check the Console tab:
- Red errors are runtime exceptions
- Yellow warnings often indicate React-specific issues (key prop, deprecated APIs)
- Check the "Preserve log" checkbox to retain errors across navigations
- Filter by "Errors" to cut through noise
Checking Terminal / Server Logs
Next.js server errors appear in the terminal where next dev or next start is running:
- Server component errors
- API route errors
- Middleware errors
- Build-time errors
Look for [next] prefixed lines and the full error output including any cause property.
Strategic console.log at Component Boundaries
Place logs at the entry point of components to trace data flow:
export function UserList({ users }: { users: User[] }) {
console.log('[UserList] render', { users, type: typeof users, isArray: Array.isArray(users) });
return (
<ul>
{users.map((user) => (
<li key={user.id}>{user.name}</li>
))}
</ul>
);
}
Always log the type and shape of data, not just the value. undefined and null look the same when logged alone.
Using the debugger Statement
Drop a debugger statement to trigger a breakpoint when DevTools is open:
export async function getUser(id: string): Promise<User> {
const response = await fetch(`/api/users/${id}`);
const data = await response.json();
debugger;
return data;
}
Remove debugger statements before committing. Consider a lint rule (no-debugger) to catch these.
TypeScript: Reading tsc Errors
Run tsc --noEmit to get the full list of type errors without producing output files:
pnpm tsc --noEmit
Read TypeScript errors from the bottom up. The last line of a TS error is usually the most specific:
src/components/UserList.tsx:14:5 - error TS2322: Type 'string' is not assignable to type 'number'.
14 count: userId,
~~~~~
The expected type comes from property 'count' declared here:
interface Props {
count: number;
}
The "expected type comes from" section tells you where the constraint originates.
Using node --inspect for Server-Side Debugging
Start the Next.js dev server with the Node.js inspector:
NODE_OPTIONS='--inspect' pnpm next dev
Then open chrome://inspect in Chrome and connect to the Node.js target. You can set breakpoints in server components, API routes, and middleware.
Phase 2: Common Bug Patterns (Next.js Focus)
Hydration Mismatch
Symptom: "Text content does not match server-rendered HTML" or "Hydration failed because the server-rendered HTML didn't match the client."
Cause: The HTML rendered on the server differs from what the client renders on first pass.
Diagnostic:
export function Timestamp() {
return <span>{Date.now()}</span>;
}
export function Timestamp() {
const [time, setTime] = useState<number | null>(null);
useEffect(() => {
setTime(Date.now());
}, []);
if (time === null) return <span>Loading...</span>;
return <span>{time}</span>;
}
Common causes and fixes:
- Browser extensions injecting HTML — test in incognito mode
typeof window !== 'undefined' checks in render — move to useEffect
- Dynamic imports that need
ssr: false:
import dynamic from 'next/dynamic';
const MapComponent = dynamic(() => import('./Map'), { ssr: false });
"Cannot read properties of undefined"
Diagnostic approach: Trace the undefined value up the call chain.
export function ProfileCard({ user }: { user: User }) {
if (!user) {
console.error('[ProfileCard] user is undefined — check parent component');
return null;
}
return <div>{user.email}</div>;
}
Then check the parent that passes user as a prop. The issue is almost always that the data has not loaded yet or an API returned an unexpected shape.
Unhandled Promise Rejection
Symptom: UnhandledPromiseRejection or Unhandled Runtime Error with an async operation.
Common causes:
function handleSubmit() {
saveUser(formData);
}
async function handleSubmit() {
try {
await saveUser(formData);
} catch (error) {
console.error('Failed to save user:', error);
setError('Failed to save. Please try again.');
}
}
useEffect(async () => {
const data = await fetchData();
setData(data);
}, []);
useEffect(() => {
async function load() {
try {
const data = await fetchData();
setData(data);
} catch (error) {
console.error('Failed to load data:', error);
}
}
load();
}, []);
React Hook Rules Violation
Symptom: "Rendered more hooks than during the previous render" or "Hooks can only be called inside a function component."
Rule: Hooks must be called in the same order on every render. No conditionals, no loops, no early returns before hooks.
export function UserProfile({ userId }: { userId: string | null }) {
if (!userId) return <div>No user</div>;
const [user, setUser] = useState<User | null>(null);
}
export function UserProfile({ userId }: { userId: string | null }) {
const [user, setUser] = useState<User | null>(null);
useEffect(() => {
if (userId) {
fetchUser(userId).then(setUser);
}
}, [userId]);
if (!userId) return <div>No user</div>;
return <div>{user?.name}</div>;
}
Stale Closure
Symptom: A callback or effect reads an old value of state or props.
export function Counter() {
const [count, setCount] = useState(0);
useEffect(() => {
const id = setInterval(() => {
console.log(count);
setCount(count + 1);
}, 1000);
return () => clearInterval(id);
}, []);
return <div>{count}</div>;
}
export function Counter() {
const [count, setCount] = useState(0);
useEffect(() => {
const id = setInterval(() => {
setCount((prev) => prev + 1);
}, 1000);
return () => clearInterval(id);
}, []);
return <div>{count}</div>;
}
For non-state values, use useRef to hold a mutable reference:
const callbackRef = useRef(onSave);
callbackRef.current = onSave;
useEffect(() => {
callbackRef.current(data);
}, [data]);
Infinite Re-render
Symptom: "Maximum update depth exceeded" or the browser freezes.
export function UserList({ users }: { users: User[] }) {
const [sorted, setSorted] = useState<User[]>([]);
setSorted(users.sort());
return <ul>{sorted.map((u) => <li key={u.id}>{u.name}</li>)}</ul>;
}
export function UserList({ users }: { users: User[] }) {
const sorted = useMemo(() => [...users].sort((a, b) => a.name.localeCompare(b.name)), [users]);
return <ul>{sorted.map((u) => <li key={u.id}>{u.name}</li>)}</ul>;
}
useEffect(() => {
fetchData({ page: 1, limit: 10 });
}, [{ page: 1, limit: 10 }]);
const page = 1;
const limit = 10;
useEffect(() => {
fetchData({ page, limit });
}, [page, limit]);
Module Not Found
Diagnostic steps:
- Check path aliases in
tsconfig.json:
{
"compilerOptions": {
"paths": {
"@/*": ["./src/*"]
}
}
}
- Check if the import is server-only being used in a client component:
import { db } from '@/lib/db';
- Check barrel exports — a barrel file re-exporting a server module can break client bundles:
export { db } from './db';
export { cn } from './utils';
import { cn } from '@/lib/utils';
API Route Errors
export async function POST(request: Request) {
const body = request.body;
}
export async function POST(request: Request) {
const body = await request.json();
}
export async function GET() {
return { users: [] };
}
import { NextResponse } from 'next/server';
export async function GET() {
return NextResponse.json({ users: [] });
}
Check auth middleware is not silently blocking requests. Add logging in middleware:
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
export function middleware(request: NextRequest) {
console.log('[middleware]', request.method, request.nextUrl.pathname);
}
Build vs Runtime Errors
Some errors only appear in production builds. Always test with:
pnpm next build && pnpm next start
Common causes of build-only errors:
- Environment variables missing in production (not prefixed with
NEXT_PUBLIC_ for client access)
- Dynamic imports with expressions that cannot be statically analyzed
- Edge runtime incompatibility (Node.js APIs not available):
import { readFileSync } from 'fs';
Type Errors
as casting hiding real issues:
const user = apiResponse as User;
import { z } from 'zod';
const UserSchema = z.object({
id: z.string(),
name: z.string(),
email: z.string().email(),
});
const result = UserSchema.safeParse(apiResponse);
if (!result.success) {
console.error('Invalid user data:', result.error.flatten());
throw new Error('Invalid user data from API');
}
const user = result.data;
strictNullChecks violations:
const users = await getUsers();
const firstEmail = users[0].email;
const firstEmail = users[0]?.email ?? 'No email';
if (users.length === 0) {
throw new Error('No users found');
}
const firstEmail = users[0].email;
Generic inference failures:
const result = useQuery({ queryKey: ['users'], queryFn: getUsers });
const result = useQuery<User[]>({ queryKey: ['users'], queryFn: getUsers });
Phase 3: Diagnostic Techniques
React DevTools
Install the React DevTools browser extension and use these features:
- Components tab: Inspect the component tree, view props and state for any component, and identify which component owns a piece of state.
- Profiler tab: Record a session, then review which components re-rendered and why.
- Highlight updates: Enable "Highlight updates when components render" in DevTools settings to visually see unnecessary re-renders.
Network Tab
For API debugging, use the browser Network tab:
- Filter by
Fetch/XHR to see API calls
- Check the Status column for non-200 responses
- Click a request to see the full Request Headers, Request Body, and Response Body
- Look for CORS errors:
Access-Control-Allow-Origin header missing
- Check Timing tab to identify slow requests
React.Profiler for Performance Debugging
Wrap suspect components to measure render times:
import { Profiler } from 'react';
function onRender(
id: string,
phase: 'mount' | 'update',
actualDuration: number,
) {
if (actualDuration > 16) {
console.warn(`[Profiler] ${id} ${phase} took ${actualDuration.toFixed(2)}ms`);
}
}
export function App() {
return (
<Profiler id="UserDashboard" onRender={onRender}>
<UserDashboard />
</Profiler>
);
}
why-did-you-render for Unnecessary Re-renders
Install and configure to detect avoidable re-renders:
import React from 'react';
if (process.env.NODE_ENV === 'development') {
const { default: whyDidYouRender } = await import(
'@welldone-software/why-did-you-render'
);
whyDidYouRender(React, {
trackAllPureComponents: true,
});
}
Then tag specific components:
function UserList({ users }: { users: User[] }) {
}
UserList.whyDidYouRender = true;
Next.js Specific Diagnostics
Inspect the build output:
ls -la .next/
Get environment info:
pnpm next info
This prints Next.js version, React version, Node.js version, OS, and other relevant environment details useful for bug reports.
NODE_OPTIONS with Next.js Dev Server
Start the dev server with the Node.js debugger attached:
NODE_OPTIONS='--inspect' pnpm next dev
Open chrome://inspect in Chrome, click "Open dedicated DevTools for Node", and you can:
- Set breakpoints in server components and API routes
- Step through middleware execution
- Inspect server-side variables and state
Phase 4: Fix and Verify
Write a Failing Test First
Before writing any fix, reproduce the bug in a test using Vitest and React Testing Library:
import { render, screen } from '@testing-library/react';
import { describe, it, expect } from 'vitest';
import { UserList } from '../UserList';
describe('UserList', () => {
it('handles undefined users without crashing', () => {
const { container } = render(<UserList users={undefined} />);
expect(container).toBeTruthy();
});
it('renders an empty list when users array is empty', () => {
render(<UserList users={[]} />);
expect(screen.queryByRole('listitem')).toBeNull();
});
});
Fix at the Source, Not the Symptom
export function UserList({ users }: { users: User[] }) {
try {
return <ul>{users.map((u) => <li key={u.id}>{u.name}</li>)}</ul>;
} catch {
return <div>Something went wrong</div>;
}
}
export function UserList({ users }: { users: User[] }) {
if (!users || users.length === 0) {
return <p>No users found.</p>;
}
return (
<ul>
{users.map((user) => (
<li key={user.id}>{user.name}</li>
))}
</ul>
);
}
Run Type Checking After Fix
pnpm tsc --noEmit
This ensures your fix does not introduce new type errors. Run this before committing.
Run the Test Suite
pnpm vitest run
Or for a specific test file:
pnpm vitest run src/components/__tests__/UserList.test.tsx
Verify in Both Dev and Production Build
Some bugs only appear in production. Always check both:
pnpm next dev
pnpm next build && pnpm next start
Common differences:
- Server components are fully streamed in production
- Environment variables behave differently
- CSS modules and Tailwind may purge differently
- Dynamic imports behave differently with code splitting
If 3+ Fixes Fail, Question the Architecture
If you have attempted three fixes and none resolve the issue, the bug is likely architectural. Stop patching and reassess:
- Is the data flowing through the right components?
- Is state owned at the correct level?
- Should this be a server component instead of client (or vice versa)?
- Is the API contract correct?
The 3-Strike Rule
If three fix attempts fail:
- Stop. Do not attempt a fourth fix.
- Document what you tried and why each attempt failed.
- Identify the architectural issue. The bug is a symptom of a deeper design problem.
- Discuss before proceeding. Present the findings and propose a structural change.
Continuing to apply surface-level fixes to an architectural problem creates technical debt and makes the codebase harder to maintain.
Quick Reference
| Error Message | Likely Cause | Diagnostic Step | Common Fix |
|---|
Cannot read properties of undefined (reading 'X') | Data not loaded yet or wrong shape | Add console.log before access, check parent component | Add null check / optional chaining / loading state |
Text content does not match server-rendered HTML | Hydration mismatch | Compare server HTML (view source) with client render | Move client-only logic to useEffect, use dynamic with ssr: false |
Rendered more hooks than during the previous render | Conditional hook or early return before hooks | Check for if/return before any hook call | Move all hooks above any conditional returns |
Maximum update depth exceeded | setState called during render or infinite useEffect loop | Add console.log in the component body and useEffect | Use useMemo for derived state, fix dependency arrays |
Unhandled Runtime Error: [async error] | Missing await or unhandled promise rejection | Check for missing await keywords, add .catch() | Add try/catch around async calls, await all promises |
Module not found: Can't resolve 'X' | Wrong import path, server module in client | Check tsconfig.json paths, check 'use client' boundary | Fix import path, move server imports to server components |
Type 'X' is not assignable to type 'Y' | TypeScript type mismatch | Run pnpm tsc --noEmit, read full error chain | Fix the type at its source, avoid as casting |
Objects are not valid as a React child | Rendering an object/array directly instead of JSX | console.log the value being rendered | Map arrays to JSX, stringify objects, extract primitive values |
CORS error / NetworkError | API request blocked by browser CORS policy | Check Network tab for preflight OPTIONS request | Add CORS headers in API route or Next.js config headers() |
NEXT_PUBLIC_ env var undefined | Env var not prefixed for client access | Check .env.local and variable naming | Prefix with NEXT_PUBLIC_ for client-side access |