Web developers frequently encounter a frustrating error that can bring a halt to JavaScript-enhanced user interfaces: “jQuery is not defined.” This error, while simple in appearance, can stem from a range of issues that typically revolve around incorrect load order, improper use of the defer attribute, or compatibility issues. Understanding these root causes and how to resolve them is essential for ensuring seamless user experiences and maintaining functional web applications.
TL;DR: The “jQuery is not defined” error usually occurs when scripts are loaded in the wrong order — a script relying on jQuery is run before jQuery is actually loaded. The use of the defer attribute can prevent this problem when used correctly. Additionally, compatibility issues such as loading jQuery from improperly configured CDNs or mixing different libraries can cause this error. Judicious debugging and script management strategies can help eliminate this common JavaScript error.
Understanding the Error: “jQuery is not defined”
When the browser attempts to execute a script that uses jQuery before the jQuery library itself has fully loaded, it throws the "jQuery is not defined" error. This error halts script execution and leads to non-functional interactive components of the website, such as sliders, modals, or AJAX requests.
At a technical level, the error means the browser does not recognize what jQuery is at the time a script tries to use it — it simply hasn’t seen a definition for it yet.

Common Causes of the Error
Let’s explore the most frequent reasons this error might appear:
- Incorrect script load order: If your custom JavaScript loads before jQuery, references to jQuery will fail.
- Deferred or asynchronous load gone wrong: Improper use of the
deferorasyncattributes can lead to unexpected load timing. - CDN or file not found: If the jQuery file fails to load (due to a 404 error or CDN issues), dependent code will fail too.
- Conflicts with other libraries: Other JavaScript frameworks can interfere with jQuery, especially if they override the
$variable. - Missing or incorrect ‘noConflict’ usage: jQuery supports a
noConflict()function to avoid namespace collisions. Improper usage can result in undefined jQuery references.
Resolving the Load Order Issue
Load order is the most common culprit behind this error. You must ensure that the jQuery library is loaded before any scripts that use it. Here’s how a correct script order should look:
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="/js/my-script.js"></script>
In this example, my-script.js depends on jQuery and will only be evaluated by the browser after jQuery has been loaded.
Using defer and async Correctly
To prevent render-blocking, developers often use async or defer attributes on <script> tags. However, these attributes control how the scripts load and can inadvertently cause dependencies to break.
defer: Ensures that the script is executed after the document has been parsed. Scripts withdefermaintain order relative to each other.async: Executes scripts as soon as they’re available, potentially out of order.
To ensure jQuery loads first, use defer consistently and preserve the order:
<script src="https://code.jquery.com/jquery-3.6.0.min.js" defer></script>
<script src="/js/my-script.js" defer></script>
Do not mix async and defer unless you understand their implications. Mixing may result in unpredictable execution order.
Checking for CDN and File Path Issues
A simple mistake in the URL, a missing file on your server, or a CDN outage can cause the script to fail entirely. If jQuery is hosted externally, failure in loading the file means jQuery will not be defined.
To check this:
- Open your browser’s Developer Tools.
- Go to the Network tab and reload the page.
- Look for the jQuery script request and verify its status is 200 (OK).
If the file returns 404 or any other error, you’ll need to fix the path or choose a different CDN.
Handling Library Conflicts and Compatibility
If you’re using another JavaScript library that also uses the $ sign (such as Prototype or MooTools), jQuery’s shortcut alias might be overwritten. You can mitigate this with jQuery.noConflict().
var $j = jQuery.noConflict();
// Now use $j instead of $ throughout your code
$j(document).ready(function() {
$j("#example").text("jQuery works without conflicts!");
});
Additionally, ensure all libraries are compatible with each other and that jQuery is not included multiple times with different versions, which can also result in undefined behavior.
Best Practices to Avoid “jQuery is Not Defined”
To summarize, here are some key best practices:
- Always place the jQuery script before your custom scripts.
- Use
deferwhen loading jQuery and script files, but only when needed, and maintain order. - Test CDN availability or host jQuery locally for reliability.
- Avoid mix-and-match usage of multiple frameworks that clash with jQuery unless necessary.
- Use
jQuery.noConflict()if integrating with libraries that use$.
Advanced Debugging Tips
If the error persists, try the following:
- Insert a
console.log(jQuery);in your scripts to check if jQuery is defined before use. - Use the
DOMContentLoadedevent rather than inline scripts to delay execution:
document.addEventListener("DOMContentLoaded", function() {
// jQuery usage here
});
This ensures your scripts execute only after HTML and scripts are fully parsed.
Conclusion
The “jQuery is not defined” error, while common, is straightforward to debug once you understand JavaScript loading behavior. Correctly ordering your scripts, judiciously using loading attributes, and checking for compatibility issues can save hours of frustration. With best practices in mind, your sites can leverage jQuery’s power without runtime hiccups.
FAQ
- Q: What does “jQuery is not defined” mean?
A: It means that the browser has not loaded the jQuery library before attempting to use it in your code. - Q: Can I fix this by just placing scripts in the footer?
A: Placing scripts at the end of<body>does help, but you still need to ensure the correct order is maintained. - Q: Should I use
asyncordeferwith jQuery?
A: Usedeferfor jQuery and any dependent scripts to ensure the proper load order. Avoidasyncif script order is important. - Q: Will switching to a local jQuery file help?
A: Yes, hosting jQuery on your server removes dependence on external CDNs and prevents issues caused by connectivity problems or CDN outages. - Q: What if I have multiple versions of jQuery?
A: Avoid including multiple versions. Choose one and check all plugins or scripts that depend on a specific version to ensure compatibility.
