ProjectStack
npm

TypeError: Cannot read properties of undefined

You're trying to access a property or call a method on a variable that is undefined. The variable was never assigned a value, or a function returned undefined instead of an object.

Common causes

  • A function returned undefined instead of the expected object
  • An array index is out of bounds — accessing arr[5] when the array has 3 items
  • An async operation hasn't completed yet and the result is still undefined
  • A typo in a variable or property name

How to fix it

  1. Add a null check before accessing the property: if (obj && obj.property)
  2. Use optional chaining to safely access nested properties: obj?.property?.nested
  3. Log the variable before the failing line to see its actual value: console.log(obj)
  4. Check that any async data fetching has completed before accessing the result

Example

TypeError: Cannot read properties of undefined (reading 'name') at getUserName (/home/user/app.js:15:20) at Object.<anonymous> (/home/user/app.js:30:5)

Accessing user.name when getUser() returned undefined because the user wasn't found

Have a different error?

Paste any error message into the Error Translator to get an instant explanation.