JavaScript TypeError — Cannot Convert undefined or null to Object
Fix "Cannot convert undefined or null to object" from Object.keys, spread, or destructuring by defaulting the input and validating at the boundary.
Symptoms
- TypeError: Cannot convert undefined or null to object at Object.keys
- A spread of a possibly-missing config throws
- Destructuring a null API response fails
Possible causes
- Object.keys/values/entries is called with undefined or null
- Spread or destructuring targets a value that is not an object
- JSON.parse returned null and is treated as an object
Diagnosis steps
Reproduce and read the call in the stack
node -e "console.log(Object.keys(undefined))"Interpretation
The stack frame names the exact function that received null/undefined.
Confirm the source value
console.log(typeof config, config)Interpretation
Seeing undefined versus null versus a primitive tells you which default to apply.
Root cause
Object.keys and similar statics require an argument that can be coerced to an object. undefined and null cannot, so they throw TypeError; spreading and destructuring share the same requirement.
Fix
Default the input (config ?? {}), validate before use, and never treat a parsed value as an object without checking its type. Prefer optional chaining for reads.
Risk note
Silently defaulting to {} can hide a genuinely missing config; log when the default path is taken.
Verify the fix
Rerun with the previously failing input and confirm no throw; add tests for undefined, null, and a valid object.
Symptoms
When you hit “JavaScript TypeError — Cannot Convert undefined or null to Object”, guessing wastes time. The guide below follows evidence: reproduce the symptom, run targeted diagnostics, identify the root cause, apply the fix, and verify it with concrete checks.
Observed symptoms include:
- TypeError: Cannot convert undefined or null to object at Object.keys
- A spread of a possibly-missing config throws
- Destructuring a null API response fails
Reproduce and Diagnose
Reproduce the failure in isolation before changing any code. Work through the checks below in order; each one narrows the search space.
Step 1: Reproduce and read the call in the stack
node -e "console.log(Object.keys(undefined))"
The stack frame names the exact function that received null/undefined.
Step 2: Confirm the source value
console.log(typeof config, config)
Seeing undefined versus null versus a primitive tells you which default to apply.
Likely contributors, in order of frequency:
- Object.keys/values/entries is called with undefined or null
- Spread or destructuring targets a value that is not an object
- JSON.parse returned null and is treated as an object
Root Cause
Object.keys and similar statics require an argument that can be coerced to an object. undefined and null cannot, so they throw TypeError; spreading and destructuring share the same requirement.
The Fix
Default the input (config ?? {}), validate before use, and never treat a parsed value as an object without checking its type. Prefer optional chaining for reads.
Watch out: Silently defaulting to {} can hide a genuinely missing config; log when the default path is taken.
function summarize(config) {
const cfg = config ?? {}; // default before object ops
const keys = Object.keys(cfg);
const { retries = 3 } = cfg; // safe destructure
return { keys, retries };
}
const parsed = JSON.parse(text); // may be null
if (parsed && typeof parsed === 'object') {
console.log(Object.keys(parsed));
}
Verify and Prevent
Rerun with the previously failing input and confirm no throw; add tests for undefined, null, and a valid object.
Once verified, add a regression test that reproduces the original symptom so the failure cannot silently return.
This class of failure is usually environmental rather than pure logic, so capture the exact command, the environment variables, and the working directory in your bug report. Reproduce it in the smallest possible script, then widen the scope only once the minimal case is understood. A short written checklist for this failure mode saves hours the next time it appears.