node type_error ai_generated true

TypeError: Cannot read properties of undefined (reading 'property')

ID: node/typeerror-cannot-read-undefined

Also available as: JSON · Markdown
88%Fix Rate
90%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
20 active

Root Cause

Accessing property on undefined/null. The #1 JavaScript runtime error worldwide.

generic

Workarounds

  1. 92% success Trace where the variable becomes undefined — check async timing, missing return, wrong key name
    // Add console.log before the failing line:
    console.log('obj is:', typeof obj, obj);
    // Common causes: forgot await, wrong destructuring key, API returned null

    Sources: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cant_access_property

  2. 85% success Use nullish coalescing (??) with explicit defaults at the source, not at every usage
    const value = response?.data ?? defaultValue;

    Sources: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing

Dead Ends

Common approaches that don't work:

  1. Add optional chaining (?.) everywhere 60% fail

    Masks bugs — undefined propagates silently through the chain

  2. Default to empty object with || {} 55% fail

    Fails for falsy values (0, '', false)

Error Chain

Frequently confused with: