"ReferenceError: $ is not defined" happens when your code tries to use $ before jQuery has finished loading, when jQuery failed to load (wrong path, network/CDN issue, or script order), or when another library/scope conflict overrides $ (such as jQuery.noConflict() or running inside an ES module where $ isn’t imported). To fix it, ensure jQuery loads before any code that calls $, verify the script URL/import resolves cleanly, or alias jQuery / pass $ safely.
1. Script Load Order, Bundlers/Modules, and Safe Guards
Most often, a snippet using $ runs inline or in a file loaded above the jQuery script, or runs inside a module/bundler where jQuery wasn’t imported or attached to window.
// 1. In bundlers / ES modules: import jQuery explicitly instead of assuming global $
import $ from "jquery";
// 2. If another library claims $, alias jQuery locally inside a ready block
jQuery(function ($) {
// Inside here, $ safely refers to jQuery
$("#status").text("jQuery is ready");
});
// 3. Defensive check before calling jQuery code in shared/partial scripts
if (typeof window.jQuery === "function") {
window.jQuery(function ($) {
$(".js-init").addClass("is-ready");
});
} else {
console.warn("jQuery is not loaded yet.");
}
- Check load order: Any script using
$must execute after jQuery has loaded—not in a<head>script above jQuery unless wrapped in a handler that runs after load. - Watch out for
defer/type="module"mismatches: If jQuery is deferred or loaded as a module, inline scripts withoutdeferorDOMContentLoadedwill run first and throwReferenceError: $ is not defined.
2. Diagnosing Broken Paths, CDNs, and noConflict() Collisions
If $ is still undefined after checking load order, inspect the browser’s Network and Console tabs to see whether the jQuery file returned 404/blocked or whether another script called $.noConflict().
// Check whether window.jQuery exists vs. window.$
console.log({
hasJQuery: typeof window.jQuery !== "undefined",
hasDollar: typeof window.$ !== "undefined"
});
// Restore $ if another library overwrote it and you want jQuery to reclaim $
if (typeof window.jQuery === "function" && typeof window.$ === "undefined") {
window.$ = window.jQuery;
}
- 404 or blocked script: A typo in a local path or a blocked/offline CDN request means
window.jQuerynever loads at all—fix the path/URL or serve a local copy. noConflict()or another library using$: Prototype, older plugins, orjQuery.noConflict()can release or overwrite$whilejQuerystill exists.
3. Common Traps to Avoid
- Placing
$("...")code above the jQuery<script>tag: Top-level code executes immediately during HTML parsing before later scripts are fetched or evaluated. - Using$inside<script type="module">without importing it: Module scripts have their own scope and do not automatically pull globals unless imported or accessed viawindow.$. - Ignoring404orERR_BLOCKED_BY_CLIENTerrors on the jQuery request: A singleReferenceErroroften masks an earlier failed script request right above it in the console. — TL;DR: Fix"ReferenceError: $ is not defined"by loading jQuery before any code using$, verifying the jQuery URL/import loads with no404/network errors, importing$in ES modules/bundlers, or usingjQuery(function ($) { ... })when$conflicts with another library. Is this happening on a plain HTML/script page, inside a bundler/framework (like Webpack, Vite, or React), or alongside another library using$?
