Working Code
First, let's create code with an intentional bug:
> Create a bug-demo.js file with this code:
> const users = [{ name: "Alice", age: 30 }, { name: "Bob", age: 25 }];
> console.log(users.find(u => u.name === "Charlie").age);
Now run it:
> Run node bug-demo.js
A TypeError: Cannot read properties of undefined error occurs. Claude sees the error and immediately analyzes the cause:
> Fix this error
Claude identifies that find can return undefined and applies optional chaining or a null check.
Try It Yourself
Let's try a more realistic scenario. Assume an error occurs in a real project:
> Run npm run build
If there's a build error, Claude reads the error log and analyzes the cause. If your project has no errors, create one:
> Intentionally break an import path in server.js, then run it to see the error.
After seeing the error:
> Fix this error
"Why?" — Why Claude Code Excels at Debugging
Claude Code doesn't just look at the error message. Because it knows the full project context:
- It checks related files, not just the file where the error occurred.
- It traces import relationships to find the root cause.
- It detects mismatches with configuration files (tsconfig, package.json, etc.).
Effective Debugging Request Patterns
> Run npm test and fix the failing tests
> Analyze this error log: [paste error message]
> The users API returns a 500 error — find the cause
> Add console.log statements to this function to trace the data flow
Debugging Workflow
Error occurs → Ask Claude to run → Automatic error analysis → Fix suggestion → Approve → Re-run to verify
For complex bugs that aren't resolved in one pass, take a step-by-step approach:
> Analyze the cause of this error. Don't fix it yet.
Once you understand the cause:
> OK, fix it using the ~ approach
Deep Dive
What if Claude suggests the wrong fix?
Claude makes mistakes too. When a fix is wrong:
- Reject it with
n. - Explain why it's wrong: "That fix breaks another feature. Try ~ instead."
- Give additional context: "This function needs to work this way because ~."
Working with Claude is iterative. Instead of expecting a perfect answer on the first try, think of it as narrowing things down together.
- Intentionally create an error in your project (import typo, referencing a nonexistent variable, etc.).
- Ask Claude to "run this project."
- When the error occurs, read Claude's analysis and approve the fix.
- Re-run to confirm the problem is resolved.
Q1. What's the most effective approach to debugging with Claude Code?
- A) Just say "fix the error"
- B) Show the error message and provide context
- C) Fix the code yourself and only ask Claude to confirm
- D) Start a new project when there's an error