How to Debug a Code Error With ChatGPT
Paste a stack trace, get the likely cause, and walk through a fix step by step instead of guessing.
You hit an error, the message is cryptic, and the first three search results are five years out of date. ChatGPT is good at reading a stack trace, telling you what it usually means, and suggesting a concrete fix. The quality of its answer depends almost entirely on how much of the error and code you give it.
- The full error message or stack trace, not a paraphrase
- The relevant code that triggers it (the function or block, not the whole repo)
- Your language and runtime version, plus any framework involved
Step 1: Reproduce and copy the full error
Run the failing command and copy the entire output, including the line numbers and the bottom-most error. People often paste only the last line, but the call stack above it is where the real clue lives.
Step 2: Paste the error plus the code that throws it
Give the model both halves: the error and the source it points to. Line 12 of list.js is where to look, so include that function. State what you expected to happen.
I get this error in Node 20:
TypeError: Cannot read properties of undefined (reading 'map') at renderList (list.js:12)
Here is renderList:
function renderList(data) {
return data.items.map(i => `<li>${i}</li>`).join('');
}
I call renderList(response). Why is it undefined and how do I fix it safely?Step 3: Read the explanation, then the fix
A good answer names the cause (data.items is undefined because response has no items key) before showing code. If it jumps straight to a patch, ask why did this happen so you understand it and do not just paste a fix you cannot maintain.
Step 4: Test the fix and confirm the shape
Apply the suggested change, rerun, and add a log to verify the data really looks like the model assumed. If the error moves rather than disappears, paste the new error back into the same chat so it keeps the context.
Result
Instead of guessing for twenty minutes, you understand that the response shape was wrong, you have a defensive fix, and you know how to confirm the data. The error is gone and you learned why it happened.
Watch related tutorials
16:12
25:00
14:00
15:00
1:42:18
28:14