207

I'm developing a user interface for my application using Bootstrap. I included jQuery (version 1.11.0) in my HTML document's <head> section, expecting it to meet Bootstrap's dependency requirements. However, when I load the page in a browser, I encounter the following error from jQuery:

Uncaught Error: Bootstrap's JavaScript requires jQuery

I've also tested jQuery version 1.9.0 and various other copies hosted on different CDNs, but I'm still facing issues. Can anyone help me identify what might be misconfigured or missing in my setup?

6
  • 37
    Include jQuery before Bootstrap... Commented Mar 26, 2014 at 10:27
  • In my case, I installed jquery before bootstrap and then, it worked fine. Just including before bootstrap didn't solve the error. Commented Aug 19, 2017 at 8:03
  • I my case, instead of adding jquery in the footer (as in bootstrap template); I added it in the header. Solved the problem. Commented Apr 14, 2020 at 5:53
  • 9
    just add <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.0/jquery.min.js" integrity="sha256-xNzN2a4ltkB44Mc/Jz3pT4iU1cmeR0FkXs4pru/JxaQ=" crossorigin="anonymous"></script> before the bootstrap import script. Copy the latest CDN here: cdnjs.com/libraries/jquery Commented Apr 26, 2020 at 6:21
  • 2
    Are you using Electron ? I've got the exact same issue while use Electron because jQuery global declaration is done in the module and not the Wndows. Force the declaration to the global windows object instead: <script>window.$ = window.jQuery = require('./contents/jquery-3.5.1/jquery-3.5.1.js');</script> <script src="./contents/bootstrap-4.5.3-dist/js/bootstrap.min.js"></script> Solution found here: link Commented Nov 20, 2020 at 13:30

14 Answers 14

402

Try this

Change the order of JS files. It should be like below.

<script src="js/jquery-1.11.0.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/wow.min.js"></script>
Sign up to request clarification or add additional context in comments.

5 Comments

Doesn't work for me. I have jQuery before Bootstrap and get the error anyway!
worked but got a horizontal scrollbar at the bottom
If it's not working from this.. it might be that jQuery is installed via Bower and so you might need to look under bower_components/jquery/dist/jquery.js .. the "dist" part being what I had overlooked.
Worked for me for a Django-Oscar project. This error starting showing up out-of-the-blue. I suppose messing with a completely unrelated code can change the rendering sequence and trigger this error.
apparently I did not notice I have bootstrap.js included twice - first time it was way before query
115

If you're in a Browser-Only environment, use SridharR's solution.

If you're in a Node/CommonJS + Browser environment (e.g. electron, node-webkit, etc..); the reason for this error is that jQuery's export logic first checks for module, not window:

if (typeof module === "object" && typeof module.exports === "object") {
    // CommonJS/Node
} else {
    // window
}

Note that it exports itself via module.exports in this case; so jQuery and $ are not assigned to window.

So to resolve this, instead of <script src="path/to/jquery.js"></script>;

Simply assign it yourself by a require statement:

<script>
    window.jQuery = window.$ = require('jquery');
</script>

NOTE: If your electron app does not need nodeIntegration, set it to false so you won't need this workaround.

6 Comments

window.jQuery = window.$ = require('jquery'); this worked for me attempting to assemble a brunch build with jQuery 2 and Bootstrap 3. Only took me several hours to find your answer though :(. Does this requirement change in later versions of jQuery?
I don't know but since this is not a bug, I don't think so. If your electron app does not need nodeIntegration, set it to false so you won't need this workaround.
You have to install jquery using npm install jquery not with bower.
Useful to see how electron deals with the libraries different than a window based app
I still don't understand it, but Laravel uses the same approach: github.com/laravel/laravel/blob/v5.7.0/resources/js/…
|
17

You should load jQuery first because Bootstrap use jQuery features. Like this:

<!doctype html>
<html>
    <head>
        <link type="text/css" rel="stylesheet" src="css/animate.css">
        <link type="text/css" rel="stylesheet" src="css/bootstrap-theme.min.css">
        <link type="text/css" rel="stylesheet" src="css/bootstrap.min.css">
        <link type="text/css" rel="stylesheet" href="css/custom.css">
        
<!--   Shuffle your scripts HERE  -->
        <script src="js/jquery-1.11.0.min.js"></script>
        <script src="js/bootstrap.min.js"></script>
        <script src="js/wow.min.js"></script>
<!--   End  -->   

        <title>pyMeLy Interface</title>
    </head>
    <body>
        <p class="title1"><strong>pyMeLy</strong></p>
        <p class="title2"><em> A stylish way to view your media</em></p>
        <div style="text-align:center;">
            <button type="button" class="btn btn-primary">Movies</button>
            <button type="button" class="btn btn-primary btn-lg">Large button</button>
        </div>
    </body>
</html>

Comments

8

You need to add JQuery before adding bootstrap-

<!-- JQuery Core JavaScript -->
<script src="lib/js/jquery.min.js"></script>

<!-- Bootstrap Core JavaScript -->
<script src="lib/js/bootstrap.min.js"></script>

Comments

5

You have provided wrong order for JAVASCRIPT and BOOTSTRAP

by convention bootstrap must follow jquery file definition

<!-- JQuery Core JavaScript -->
<script src="lib/js/jquery.min.js"></script>
<script src="lib/js/jquery-ui.min.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="lib/js/bootstrap.min.js"></script>

Comments

3

In my case solution is really silly, and strange.

Below code was pre-populated by _Layout.cshtml file. (NOT written by me)

<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/bootstrap.min.js"></script>

But, when I verified in Scripts folder, jquery-1.10.2.min.js was not even available. Hence, replaced code like below where jquery-1.9.1.min.js is an existing file:

<script src="~/Scripts/jquery-1.9.1.min.js"></script>
<script src="~/Scripts/bootstrap.min.js"></script>

Comments

1

I'm using NodeJS and got the same error. Like the other answers, the problem was also because JQuery needed to be loaded before Bootstrap; however, because of the NodeJS characteristics, this change had to be aplied in the pipeline.js file.

The change in pipeline.js was like this:

var jsFilesToInject = [
  // Dependencies like jQuery, or Angular are brought in here
  'js/dependencies/angular.1.3.js',
  'js/dependencies/jquery.js',
  'js/dependencies/bootstrap.js',
  'js/dependencies/**/*.js',
];

I'm also using grunt to help, and it automatically changed the order in the main html page:

<!--SCRIPTS-->
  <script src="/js/dependencies/angular.1.3.js"></script>
  <script src="/js/dependencies/jquery.js"></script>
  <script src="/js/dependencies/bootstrap.js"></script>
  <!-- other dependencies -->
<!--SCRIPTS END-->

Hope it helps! You didn't said what your environment was, so I decided to post this answer.

Comments

1

On official docs: https://electronjs.org/docs/faq#i-can-not-use-jqueryrequirejsmeteorangularjs-in-electron

<head>
  <script>
  window.nodeRequire = require;
  delete window.require;
  delete window.exports;
  delete window.module;
  </script>
  <script type="text/javascript" src="jquery.js"></script>
</head>

1 Comment

I fixed it on angular.json with the code " "scripts": ["node_modules/jquery/dist/jquery.min.js", "node_modules/bootstrap/dist/js/bootstrap.min.js"]"
1

If you are using require.js than need to add window.$ = window.jQuery = require('jquery') in app.js

OR you can make dependent file load after parent file loaded.. such as below concept

require.config({
    baseUrl: "scripts/appScript",
    paths: {
        'jquery':'jQuery/jquery.min',
        'bootstrap':'bootstrap/bootstrap.min' 
    },
   shim
    shim: {
        'bootstrap':['jquery'],
        },

    // kick start application
    deps: ['app']
});

Above code shown that bootstrap is dependent on Jquery so i have added in shim and make it dependent

Comments

1

After struggling with this problem I took three steps:

  1. Use jQuery versions anywhere between 1.9.0 and 3.0.0
  2. Declare the jQuery file before declaring the Bootstrap file
  3. Declare these two script files at the bottom of the <body></body> tags rather than in the <head></head>. These worked for me but there may be different behaviors based on the browser you are using.

Comments

0

If your code looks good, verify that jQuery successfully loaded to your server. In my case, the jQuery files were published to the server by my IDE, but the web server only had a 0 KB file stub. With no content, the server failed to serve the file to the browser.

After re-publishing the file and verifying that the server actually received the whole file, then my web page stopped giving me the error.

Comments

0

You need to add JQuery js before adding bootstrap js file, because BootStrap use jqueries function. So make sure to load first jquery js and then bootstap js file.

<!-- JQuery Core JavaScript -->
<script src="app/js/jquery.min.js"></script>

<!-- Bootstrap Core JavaScript -->
<script src="app/js/bootstrap.min.js"></script>

Comments

0

If this occurs IE intranet only, check compatibility settings and uncheck "Display intranet sites in Compatibility mode" .

IE-->settings-->compatibility

enter image description here

Comments

0

I had tried almost all the above methods.

Finally fixed it by including the

script src="{%static 'App/js/jquery.js' %}"

just after loading the staticfiles i.e {% load staticfiles %} in base.html

Comments