ChatGPTIntermediate

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.

7 minIntermediate

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.

zsh - project
$node index.js
TypeError: Cannot read properties of undefined (reading 'map')
at renderList (/app/src/list.js:12:18)
at Object.<anonymous> (/app/index.js:5:1)
copy all of this, not just the first line
$

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.

prompt.txt
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.

ChatGPT - Diagnosis
You
Why is data.items undefined and how do I fix it safely?
Agent
data.items is undefined because the response object does not have an items property at call time. Guard it: function renderList(data) { return (data?.items ?? []).map(i => `<li>${i}</li>`).join(''); } Also log response before the call to confirm its shape.

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.

zsh - project
$node index.js
<li>alpha</li><li>beta</li>
no crash, list renders
$
Do not paste secrets
Strip API keys, tokens, passwords, and customer data out of code before pasting it. Replace them with placeholders like YOUR_KEY so you never send credentials into a chat.

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

Tags
#coding#debugging#errors#development