How to fix the “jQuery is not defined” error

Something has broken on your site, you’ve checked your browser console for errors and found the following error:

jQuery is not defined

jquery not defined error

The error means that jQuery is required by another script, but due to the optimization, it’s not available when that initial script runs, and therefore triggers an error.

Check if this error is caused by WP Rocket

Load your page while bypassing WP Rocket, by adding ?nowprocket to the URL (e.g. example.org?nowprocket), and check if the error exists there. 

If it does, WP Rocket is not the cause, and you should check for JavaScript options in other optimization plugins, or code in your functions.php on your site. 

The error isn’t there on ?nowprocket

If the error shows up only when WP Rocket is active on the page, it’s related to one of these options on the File Optimization tab:

  • Combine JavaScript
  • Load JavaScript Deferred
  • Delay JavaScript Execution

Combine JavaScript files

Add the following to the Excluded JavaScript files field:

/wp-includes/js/jquery/jquery.min.js

In the event that you are not using the version of jQuery loaded by WordPress, but are using one hosted by a 3rd party, you can find the right file to exclude by doing the following:

  • Load your site while bypassing WP Rocket, adding ?nowprocket to your URL.
  • Click the Network tab in the Developer tools of your browser
  • Filter for jquery.min.js, and if that doesn’t yield a result, try jquery.js
  • Copy the URL and paste it into the exclusion field in WP Rocket

custom jquery path

Load JavaScript Deferred

To exclude jQuery from being deferred, add the following to the Excluded JavaScript files field:

\/jquery(-migrate)?-?([0-9.]+)?(.min|.slim|.slim.min)?.js(\?(.*))?( |'|"|>|$)

This pattern will match jQuery and jQuery Migrate files, and the variations of their filenames too.

After applying these exclusions, save the changes and the problem should be resolved.

Delay JavaScript execution

To exclude jQuery from being delayed, add the following to the Excluded JavaScript files field:

\/jquery(-migrate)?-?([0-9.]+)?(.min|.slim|.slim.min)?.js(\?(.*))?( |'|"|>|$)

This pattern will catch variations on the jQuery filename, and excludes the jQuery Migrate file too, because these are co-dependent files.

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.