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:19Quick 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.
- Open the line in the stack and find the Object.keys/values/entries/assign call.
- Log the argument for the failing request: undefined usually means a missing field, null a database NULL or an API null.
- If missing is normal, default it: Object.keys(obj ?? {}).
- If it should never be missing, validate the input (schema) where it enters the app and return a clear error there.
- Check recent changes to the data source: a renamed field turns every record into undefined.
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.
Related errors
- TypeError: Cannot read properties of undefined (reading 'x')
- TypeError: Cannot destructure property 'id' of 'undefined' as it is undefined.
- TypeError: items is not iterable
More in Undefined, None and type errors
- TypeError: x is not a functionNode.js
- TypeError: Client is not a constructorNode.js
- ReferenceError: x is not definedNode.js
- ERR_INVALID_ARG_TYPE: The "path" argument must be of type string. Received undefinedNode.js
- TypeError: 'NoneType' object is not subscriptablePython
- AttributeError: 'NoneType' object has no attribute 'x'Python
- TypeError: 'NoneType' object is not iterablePython
- KeyError: 'email'Python
- TypeError: unhashable type: 'list'Python
- AttributeError: 'dict' object has no attribute 'name'Python
- IndexError: list index out of rangePython
- TypeError: unsupported operand type(s) for +: 'int' and 'str'Python
- ValueError: invalid literal for int() with base 10: ''Python
- TypeError: __init__() got an unexpected keyword argument 'proxies'Python
- UnboundLocalError: cannot access local variable 'total' where it is not associated with a...Python
- RuntimeError: dictionary changed size during iterationPython
- ValueError: too many values to unpack (expected 2)Python
- TypeError: 'str' object is not callablePython
References: MDN: Object.keys(), MDN: Object.entries()