Webflow sync, pageviews & more.
NEW

What is causing the "TypeError: Cannot convert undefined or null to object" error on multiple pages in Webflow?

TL;DR
  • Review and isolate custom code in Webflow’s Page and Project Settings, checking for object methods like Object.keys() used on undefined or null values.
  • Ensure all variables and DOM elements exist before accessing or manipulating them, and wrap page-specific scripts in conditionals to avoid running them globally.

The "TypeError: Cannot convert undefined or null to object" error in Webflow is typically caused by custom JavaScript that's trying to manipulate or access a value that hasn't been defined—or is null—when your code expects an object.

1. Check Custom Code Sections (Page or Site-Wide)

  • Go to Page Settings and Project Settings > Custom Code.
  • Look under the Before tag and Inside tag for any custom scripts.
  • Look for Object.keys(...), Object.assign(...), or Object.entries(...)—these methods throw this error if the passed value is null or undefined.

2. Identify Where undefined or null Is Being Used Improperly

  • If you’re using a variable like Object.keys(myVar) and myVar is not set yet, it will trigger this error.
  • Make sure all objects in your script are defined before being passed to object-related methods.

3. Test for DOM Element Availability

  • If your script assumes that certain elements exist on every page, but they don’t, this can also trigger the error.
  • For example: document.querySelector('.some-element').addEventListener(...) will fail if .some-element doesn’t exist. To prevent this, use a check:
  • Always check if the element exists before trying to manipulate it.

4. Check for Page-Specific Scripts Running Globally

  • If your script relies on elements or data only present on specific pages, wrap the logic in a condition to check the URL or element existence.
  • Example: Don’t run a form-validation script meant for /contact on /about.

5. Disable Scripts Temporarily to Isolate the Issue

  • In Custom Code, comment out or remove custom scripts temporarily.
  • Test the published site page-by-page to see if the error disappears. Reintroduce scripts incrementally to identify the faulty one.

6. Use Browser Dev Tools Console

  • Open DevTools (F12) and go to the Console tab.
  • The error message usually includes a line number and file, which can help you locate the specific script or line causing it.

Summary

The "TypeError: Cannot convert undefined or null to object" error in Webflow is almost always caused by custom JavaScript trying to access object methods on values that are not defined. Focus on auditing your custom scripts, ensure they only run when needed, and always check for the existence of objects or DOM elements before manipulating them.

Rate this answer

Other Webflow Questions