| name | debugging |
| description | Debug code errors systematically. Use when analyzing error messages, fixing bugs, resolving build/lint errors, or troubleshooting runtime issues. |
Debugging Skill
When to Use
- Analyzing error messages and stack traces
- Finding root causes of bugs
- Fixing runtime errors
- Resolving build/lint errors
Debugging Process
Step 1: Understand the Error
- Read the full error message
- Identify the error type (TypeError, ReferenceError, etc.)
- Find the file and line number
- Check the stack trace for call sequence
Step 2: Reproduce the Issue
- Identify the trigger conditions
- Create a minimal reproduction
- Note any environment factors
Step 3: Analyze Root Cause
- Check recent changes (git diff)
- Verify assumptions about data types
- Trace data flow through the code
- Check for edge cases
Step 4: Fix and Verify
- Make the smallest fix possible
- Add defensive checks if needed
- Test the fix thoroughly
- Check for regression
Common Error Patterns
TypeError: Cannot read properties of undefined
Cause: Accessing property on undefined/null
const name = user.profile.name;
const name = user?.profile?.name;
const name = user?.profile?.name ?? 'Unknown';
if (!user?.profile) return null;
const name = user.profile.name;
TypeError: X is not a function
Cause: Calling something that isn't a function
import { myFunction } from './utils';
import myFunction from './utils';
console.log(typeof myFunction);
class MyClass {
method = () => {};
}
ReferenceError: X is not defined
Cause: Using variable before declaration or misspelling
import { useState } from 'react';
function outer() {
const x = 1;
function inner() {
console.log(x);
}
}
const userNmae = 'John';
Async/Await Errors
async function fetchData() {
const data = await fetch('/api/data');
return data.json();
}
async function fetchData() {
try {
const response = await fetch('/api/data');
if (!response.ok) throw new Error('Fetch failed');
return await response.json();
} catch (error) {
console.error('Fetch error:', error);
return null;
}
}
React Hooks Errors
function MyComponent() {
const [state, setState] = useState(0);
const handleClick = () => {
};
}
function MyComponent({ show }) {
const [state, setState] = useState(0);
if (!show) return null;
}
Next.js Specific Errors
'use client';
import { useState } from 'react';
export function ClientComponent() {
const [count, setCount] = useState(0);
}
Next.js Build Errors
| Error | Cause | Fix |
|---|
'use client' must be first | Directive not at line 1 | Move to very first line |
Module not found: @/... | Path alias issue | Check tsconfig paths |
Hydration mismatch | Server/client HTML differs | Ensure same data |
await params | Dynamic route in Next.js 16 | Add await context.params |
Text content mismatch | Date/random on server | Use suppressHydrationWarning or move to client |
React Runtime Errors
| Error | Cause | Fix |
|---|
Element type is invalid: got undefined | Wrong import/export | Check export type matches import |
mixed up default and named imports | Import mismatch | Pages: import X from, Components: import { X } from |
Import/Export Rules:
- Pages (
app/**/page.tsx): Use export default → Import with import X from
- Components (
components/*): Use export function X → Import with import { X } from
import HeroSection from '@/components/HeroSection';
import { HeroSection } from '@/components/HeroSection';
Prisma Errors
| Error | Cause | Fix |
|---|
P1001: Can't reach database | DB not running | Start postgres container |
P2002: Unique constraint | Duplicate entry | Check unique fields |
P2025: Record not found | Delete/update missing | Check ID exists first |
PrismaClient not generated | Missing generate | Run bunx prisma generate |
P2003: Foreign key constraint | Referenced record missing | Create parent first |
Invalid prisma client | Schema changed | Run bunx prisma generate |
Debugging Tools
Console Methods
console.log('Value:', value);
console.table(arrayOfObjects);
console.trace('Call stack');
console.time('operation');
console.timeEnd('operation');
TypeScript Type Checking
console.log('Type:', typeof value);
console.log('Is array:', Array.isArray(value));
console.log('Instance:', value instanceof Date);
if (typeof value === 'string') {
}
Git for Finding Regressions
git bisect start
git bisect bad HEAD
git bisect good <known-good-commit>
git diff HEAD~5
git log --oneline -10
Fix Verification Checklist
References
references/error-handling-patterns.md - Toast notifications, form errors, error boundaries