“Cannot read properties of undefined”: trace the value, fix the producer
Read the stack trace to find the exact line, inspect the undefined value there, then fix whatever produced it.
Something you treated as an object was undefined. Open the top stack frame in your code, log the value right before the failing line, and fix whatever produced the undefined — a missing return, a wrong key, or an unexpected API shape.
Try these fixes in order
Locate the failing line in the stack trace
Read the stack trace from the top. The first frame referencing your file and line number is where the undefined value was dereferenced; frames below it show how execution got there.
Inspect the actual value
Add a console.log of the receiver just before the failing line (or set a breakpoint) and rerun. Confirm it is undefined rather than merely missing the property.
node --inspect app.jsFind what produced the undefined
Trace the value backwards: where was it assigned, returned, or parsed? Common culprits are a function path with no return, a typo in a key, or an API that changed shape.
Fix the source, not the symptom
Fix the producer: add the missing return, correct the key, or handle the new API shape. Only then decide whether a guard like ?. is appropriate for genuinely optional data.
Interpret what you see
The stack trace points at one of your files
What it suggests: The property name in quotes is what your code tried to read; the receiver was undefined. The top stack frame naming your file is the line to inspect.
Next check: Open the file and line from the first stack frame that is your code, not node internals or node_modules.
The failing expression is like getUser().name or fetchData().map
What it suggests: The value comes from a function call, so the function returned undefined instead of the expected object or array.
Next check: Log the call's return value directly, then read that function's code paths for a missing return.
The failing expression chains into response data, e.g. data.items.map
What it suggests: The value comes from parsed JSON, an API, or props, and the real data lacks the key your code assumes.
Next check: Log the whole object just before the failing line and compare its shape with your assumption.
Why this happens
- The code reads a property from a value that is undefined: an uninitialized variable, a function that returned undefined, or an object missing the expected key.
- An API response or data shape differs from what the code assumes, so a nested property path breaks at the first missing level.
Still not working?
Do not fix this by sprinkling ?. over every property access until the crash moves elsewhere. Optional chaining hides the real question: why was the value undefined? Find the producer of the bad value and fix it there; use ?. only where absence is a legitimate, expected case.
Official sources
These steps are based on the linked documentation and general diagnostics. BugMedic has not independently tested every product and system configuration. Review commands before running them.