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
Now what?!
Fortunately this is one of the easier JavaScript errors to fix :)
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.
First check that 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:
- Delay JavaScript Execution (if customized)
- Combine JavaScript
- Load JavaScript Deferred
Delay JavaScript Execution
To exclude jQuery from being deferred add the following to the Excluded JavaScript files field:
/jquery-?[0-9.](.*)(.min|.slim|.slim.min)?.js
(This pattern will catch variations on the filename)
Combine JavaScript
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, tryjquery.js
- Copy the URL and paste it into the exclusion field in WP Rocket
Load JavaScript Deferred
To exclude jQuery from being deferred add the following to the Excluded JavaScript files field:
/jquery-?[0-9.](.*)(.min|.slim|.slim.min)?.js
(This pattern will catch variations on the filename)
After applying these exclusions, save the changes and the problem should be resolved.