How to Debug Errors with AI (Without Panicking)
Something broke. The AI wrote code that doesn't work. Here's the exact workflow to fix it fast.
Every vibe coding session eventually hits a wall. The preview is blank. The console shows a red error. The AI just wrote something that doesn't work. This happens to everyone — here's how to get through it fast.
The Debugging Mindset
The #1 mistake beginners make when AI-generated code breaks: they panic and start prompting vaguely ("it's not working, fix it"). This wastes time. The AI needs information to fix problems.
Think of debugging as a loop: Observe → Report → Fix → Verify. Every cycle, you're giving the AI more specific information until the problem is solved.
Always Give AI the Full Error Message
This is the most important debugging habit. When something breaks, open the browser console (F12 → Console tab) and copy the full error message.
Then use this prompt:
I'm getting this error in the browser console: [PASTE FULL ERROR MESSAGE HERE] This happens when [DESCRIBE WHAT ACTION TRIGGERS IT]. Here is the relevant code: [PASTE THE FAILING FUNCTION OR COMPONENT] Please fix the error and explain what caused it.
Common Vibe Coding Errors & AI Fix Prompts
TypeError: Cannot read properties of undefined
Cause: You're trying to access a property of something that doesn't exist yet (often async data that hasn't loaded).
Fix the TypeError: Cannot read properties of undefined reading '[PROPERTY]'. Add a proper null check / optional chaining before accessing this property. Also add a loading state so the UI doesn't render before data is available.
CORS Error on API calls
I'm getting a CORS error when calling [API URL] from my frontend. Add the correct headers to the API response or set up a proxy. Don't break existing functionality.
Module not found / Import error
I'm getting "Module not found" for '[PACKAGE]'. Add the correct npm install command, update the import path if needed, and make sure it's used correctly.
localStorage not available (SSR error)
I'm getting "localStorage is not defined" — this is a Next.js SSR issue. Wrap all localStorage access in a typeof window !== 'undefined' check or use useEffect.
When AI Can't Fix It
Sometimes AI gets stuck in a loop — it tries to fix the error and introduces a new one. Here's what to do:
- Start fresh — Open a new chat, describe the feature you wanted, and have AI rebuild it from scratch. Often faster than debugging a broken approach.
- Simplify the feature — Tell AI to implement a simpler version first, then add complexity back.
- Ask AI to explain — "Explain exactly what this function is supposed to do, line by line." This often reveals misunderstandings.
- Search the error — Google the exact error message. Stack Overflow has the answer for 95% of common errors.
Preventing Bugs Before They Happen
The best debugging session is the one you don't need. Add these to your standard prompt:
- "Add error boundaries and loading states" — Prevents blank screens from async errors.
- "Validate all user inputs before processing" — Prevents crashes from unexpected input.
- "Add console.log statements at key points" — Makes debugging much easier if something breaks.
- "Use TypeScript strict mode" — TypeScript catches many errors before they reach production.
When in Cursor, use the @error command to paste the error directly into the AI chat. It reads your full context and usually finds the issue in one shot.