TypeError: Cannot convert undefined or null to object

Node.js · updated 2026-11-04

Thrown by Object methods that need an object, such as Object.keys(), Object.values(), Object.entries() and Object.assign(target), when the value is undefined or null. The message doesn't name the variable, so the stack line is the main clue.

TypeError: Cannot convert undefined or null to object
    at Function.entries (<anonymous>)
    at summarizeTotals (/app/dist/reports/totals.js:22:27)
    at /app/dist/routes/reports.js:48:19

Quick fix

Find the Object.* call on the stack line and check where its argument comes from. Default it at that point (Object.entries(totals ?? {})) if missing is expected, or fail with a clear error if it should always be there. Usually the value is a field of an API response, a database row or a config object that is absent for some records.

  1. Open the line in the stack and find the Object.keys/values/entries/assign call.
  2. Log the argument for the failing request: undefined usually means a missing field, null a database NULL or an API null.
  3. If missing is normal, default it: Object.keys(obj ?? {}).
  4. If it should never be missing, validate the input (schema) where it enters the app and return a clear error there.
  5. Check recent changes to the data source: a renamed field turns every record into undefined.
typescript
type Report = { totals?: Record<string, number> | null };

export function summarizeTotals(report: Report) {
  // Before: Object.entries(report.totals) throws when totals is missing or null
  return Object.entries(report.totals ?? {}).map(([name, amount]) => ({ name, amount }));
}

Why it happens

A missing field in API or database data

The code assumes every record has an object field (metadata, settings, totals), and some records have none or NULL.

An optional config section

Object.keys(config.features) when features is not set in one environment.

Object.assign with a missing target

Object.assign(existing, update) where existing was never initialized. Spreading ({ ...undefined }) is safe, which is why converting to spread also fixes it.

A function returned nothing

A helper that returns an object on one path and falls through to undefined on another, often an early return in a branch.

Other fixes

  • TypeScript with strictNullChecks flags most of these at compile time when the field is typed as optional.
  • for...in over undefined does not throw (it runs zero times), so switching to Object.entries can surface a missing value that was silently ignored before.

Same error, other wording

  • TypeError: Cannot convert undefined or null to object (at Function.keys)
  • TypeError: Cannot convert undefined or null to object (at Object.assign)
  • TypeError: can't convert undefined to object

Let your coding agent catch the next one

Runheed records this error in production with the stack trace, the breadcrumbs before it and the agent session that shipped the code, and serves it to Claude Code, Cursor or any MCP client. With it connected, you can ask:

> Find the "Cannot convert undefined or null to object" errors in production, identify which value was undefined or null at that Object.* call, and fix it where the value comes in (default or validation) rather than wrapping the call in try/catch.

Plans from $29/moSee what your agent readsSee the setupCancel any time from the billing portal

Related errors

More in Undefined, None and type errors

References: MDN: Object.keys(), MDN: Object.entries()