Debugging & Fixing with Claude Code
After this lesson, you will be able to:
- Use Claude Code to quickly diagnose errors from stack traces, build failures, and crashes
- Fix linting errors across your entire codebase in one pass
- Refactor code safely using Claude Code's read-edit-test loop, change the structure without breaking behavior
- Find and fix slow code by asking Claude Code to analyze performance bottlenecks
Before you start
#The Debugging Workflow
Debugging is often the most frustrating part of programming, staring at an error, reading through files, trying to figure out what went wrong. Claude Code can turn much of that reading into a short conversation. This lesson shows you how to feed errors to Claude Code so it gets to a useful answer faster.
#Step 1: Paste the Error
The simplest debugging prompt is pasting the error:
Fix this error:
TypeError: Cannot read properties of undefined (reading 'map')
at UserList (src/components/UserList.tsx:15:23)
at renderWithHooks (node_modules/react-dom/...)
Claude Code will:
- Read
src/components/UserList.tsxto see line 15 - Identify that
.map()is called on an undefined variable - Trace back to understand why the variable is undefined (likely a missing data fetch or uninitialized state)
- Write the fix (add a null check, fix the data fetch, or initialize the state)
- Run the build or tests to verify
Try it! Find any error in a project you are working on (or intentionally break something, add a typo to an import). Copy the full error message and paste it into Claude Code with "Fix this error:" in front. Time how long it takes from paste to fix. Most bugs are resolved in under 2 minutes.
#Step 2: Provide Context (If Needed)
If the error alone is not enough, add context:
This error started happening after I added the pagination feature.
The UserList was working fine before. Check the recent changes
to the useUsers hook in src/hooks/use-users.ts.
Context narrows the search space. Claude Code looks at the recent changes first instead of searching the entire codebase.
#Step 3: Verify the Fix
Always end with verification:
Run npm run test and npm run build to make sure the fix works
and nothing else broke.
#Common Debugging Scenarios
Tests: Verify each debugging scenario template includes error, context, and verification steps.
#Refactoring Without Breaking Things
Refactoring is one of Claude Code's strongest capabilities because the read-edit-test loop is exactly how safe refactoring should work:
Refactor the UserController to use dependency injection:
- Extract the database calls into a UserRepository class
- The controller should receive the repository via constructor
- Do NOT change any API behavior or response format
- Run the existing tests after each file change to verify nothing breaks
The key phrase is "do NOT change any behavior." This tells Claude Code to preserve the external contract while improving the internal structure.
You ask Claude Code to 'refactor src/utils.ts.' What is the biggest risk?
utils.ts, every file that imports from it might break. The mitigation: (1) tell Claude Code to keep function signatures the same, (2) ask it to run tests after each change, and (3) use plan mode first for large refactors to see all affected files.#Safe Refactoring Prompts
# Rename a variable/function across the codebase
Rename the function 'getData' to 'fetchUserProfile' in src/api/users.ts.
Update all callers across the codebase. Run tests after.
# Extract a function
Extract the email validation logic from SignupForm.tsx into a
shared utility function in src/utils/validation.ts. Use it in
both SignupForm and ProfileForm.
# Simplify complex code
Simplify the nested if-else chain in src/utils/permissions.ts.
The current behavior must be preserved exactly -- write tests
to verify the behavior BEFORE refactoring, then refactor.
#Debugging Production Issues: A Case Study
Here is a realistic production debugging session:
Claude Code
- Reads
src/app/api/search/route.ts, finds the search query handler - Reads
src/lib/search.ts, finds the search function - Identifies the issue: the query string is not URL-encoded before being used in the database query, and special characters like
+and.are interpreted as regex metacharacters - Writes the fix: properly escape regex special characters in the search query
- Adds test cases for queries with special characters
- Runs the test suite to verify
#Key Takeaways
- Paste the full error with stack trace: Claude Code traces from the error message through the stack trace to the likely root cause, then you confirm the fix with a test
- Always end debugging prompts with verification: "Run tests after fixing" ensures the fix works and nothing else broke; Claude Code iterates automatically if tests fail
- Safe refactoring requires explicit constraints: "Do not change function signatures" and "run tests after each change" prevent refactoring from breaking callers
- Specificity prevents overreach: "Fix the TypeError in UserList.tsx" is safer than "fix all errors" because it limits the scope of changes Claude Code can make
#Quick Check
What is the most effective way to debug an error with Claude Code?
A bug only reproduces in CI, not locally. What's the most productive first move with Claude Code?