Working with JavaScript often leads web developers to rely on jQuery for simplified DOM manipulation, AJAX handling, animation, and event management. However, one of the most common and frustrating errors developers encounter is the infamous “jQuery is not defined”. This error can completely halt JavaScript execution on your site and, if not resolved quickly, can lead to broken functionality or a poor user experience.
TL;DR (Too long, didn’t read)
The “jQuery is not defined” error usually indicates that the jQuery library hasn’t been loaded before your script runs. This could be due to incorrect paths, missing CDN access, scripts being called in the wrong order, network issues, or conflicts with WordPress or other frameworks. Verifying script loading order, checking console logs, and ensuring proper online/offline access to libraries usually resolve the issue.
What Does “jQuery is Not Defined” Mean?
This message is a ReferenceError that indicates your browser doesn’t recognize the jQuery object because the corresponding script responsible for defining it hasn’t been loaded or is improperly referenced. Essentially, your JavaScript code is trying to use jQuery functions before the browser knows what jQuery is.
Common Causes and Fixes for the “jQuery is Not Defined” Error
1. jQuery Is Not Loaded Before Your Script
One of the most frequent reasons this error occurs is that your custom JavaScript that uses jQuery is being loaded before the jQuery library itself. JavaScript files are executed in the order they are loaded into the HTML, so if jQuery hasn’t been defined yet, your code won’t recognize it.
Fix:
Ensure that the jQuery library is loaded before any scripts that reference it.
<!-- Correct order -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="js/custom-script.js"></script>
<!-- Incorrect order -->
<script src="js/custom-script.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
2. Incorrect jQuery Path or Missing File
Another major culprit is referencing a jQuery file that doesn’t exist or has an incorrect URL. This typically happens with self-hosted jQuery files or if the CDN link is mistyped or unavailable.
Fix:
- Double-check the path in the
srcattribute of the<script>tag. - Make sure the file actually exists on the server or that the CDN link is correct and supports HTTPS.
- Use your browser’s developer console (F12) to check the “Network” tab for HTTP 404 errors related to the jQuery file.
3. Loading jQuery After DOMContentLoaded or Dynamically
Sometimes, developers load jQuery dynamically using JavaScript after the page has already attempted to use it. If any code that relies on jQuery runs before it has actually loaded, it will result in the “not defined” error.
Fix:
- Use
deferorasynccarefully when loading scripts. - For scripts dependent on jQuery, avoid setting them to
async. - Alternatively, use an event listener or check if jQuery is loaded before executing any dependent code.
<script src="https://code.jquery.com/jquery-3.6.0.min.js" defer></script>
<script defer src="js/your-script.js"></script>
Or use this pattern:
if (typeof jQuery !== 'undefined') {
// safe to use jQuery
$(document).ready(function () {
// your code
});
} else {
console.error('jQuery is not loaded');
}
4. CDN Not Accessible Due to Network Issues
Even when you have correctly referenced the CDN, the file might not load due to network-related issues. This is especially common in development environments with unreliable internet access or in corporate firewalls that block certain external resources.
Fix:
- Check your Internet connection or DNS settings.
- Temporarily test using a self-hosted version of jQuery to rule out CDN issues.
- Use fallbacks to local copies if the CDN fails.
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
if (typeof jQuery == 'undefined') {
document.write('<script src="js/jquery-3.6.0.min.js"><\/script>');
}
</script>
5. WordPress and Plugin Conflicts
WordPress developers commonly experience the “jQuery is not defined” error due to the way WordPress loads scripts. WordPress uses a library dependency management system, and sometimes plugins or themes might deregister or incorrectly enqueue jQuery.
Fix:
- Ensure jQuery is properly enqueued in your theme or plugin:
function load_custom_scripts() {
wp_enqueue_script('jquery');
wp_enqueue_script('my-script', get_template_directory_uri() . '/js/custom-script.js', array('jquery'), null, true);
}
add_action('wp_enqueue_scripts', 'load_custom_scripts');
- Avoid hardcoding CDN links in WordPress themes if you can use enqueue functions.
- Disable conflicting plugins temporarily to isolate the issue.
Pro Tips: Diagnosing the Problem Efficiently
To effectively troubleshoot any jQuery issue, try the following:
- Use browser dev tools: Check the Console tab for errors, and the Network tab for failed resource loads.
- Use safe coding practices: Always wrap your jQuery in
$(document).ready()or use the short form:$(function() {}). - Log checkpoints: Insert
console.log()statements to track the order of execution and the availability of jQuery at various points. - Load jQuery last: In rare cases, conflicts with other libraries might require you to ensure that jQuery is loaded after other scripts.
When Nothing Else Works
On particularly complex sites or legacy projects, jQuery conflicts can arise from multiple versions being loaded or scripts that override the global $ variable. In such cases, try using the no-conflict mode:
jQuery.noConflict();
jQuery(document).ready(function ($) {
// jQuery code using $ safely within this closure
});
If you’re still having trouble after checking script order, network issues, and plugin conflicts, consider stripping your project down and adding scripts one at a time. This modular approach often helps isolate the cause.
Conclusion
The “jQuery is not defined” error, while common, is typically easy to resolve with a systematic approach. Ensuring that the jQuery library is correctly loaded, referenced in the correct order, and not interfered with by other scripts or plugins will resolve the majority of cases. Understanding how the browser processes script tags and how content management systems like WordPress handle dependencies is key to avoiding this frustrating roadblock in your development process.
Addressing these five causes and implementing the associated fixes can restore functionality and give you a deeper understanding of how script loading and dependency management work in real-world web development projects.
I’m Sophia, a front-end developer with a passion for JavaScript frameworks. I enjoy sharing tips and tricks for modern web development.