183

I am trying to use Bootstrap to make an interface for a program. I added jQuery 1.11.0 to the <head> tag and thought that was that, but when I launch the web page in a browser jQuery reports an error:

Uncaught Error: Bootstrap's JavaScript requires jQuery

I have tried using jQuery 1.9.0, I have tried with copies hosted on several CDNs, but I can't get it work. Can anybody point out what I'm doing wrong?

Munim Munna
  • 15,455
  • 6
  • 22
  • 52
  • 30
    Include jQuery **before** Bootstrap... – Adriano Repetti Mar 26 '14 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. – Stuti Verma Aug 19 '17 at 08:03
  • I my case, instead of adding jquery in the footer (as in bootstrap template); I added it in the header. Solved the problem. – Adeel Raza Azeemi Apr 14 '20 at 05:53
  • 4
    just add `` before the bootstrap import script. Copy the latest CDN here: https://cdnjs.com/libraries/jquery – Tina Lee Apr 26 '20 at 06: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: ` ` Solution found here: [link](https://ourcodeworld.com/articles/read/202/how-to-include-and-use-jquery-in-electron-framework) – Clément Nov 20 '20 at 13:30

14 Answers14

390

Try this

Change the order of 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>
Sridhar R
  • 19,414
  • 6
  • 36
  • 35
  • 101
    Doesn't work for me. I have jQuery before Bootstrap and get the error anyway! – Green Jul 15 '14 at 10:32
  • 2
    worked but got a horizontal scrollbar at the bottom – HimalayanCoder Jul 07 '15 at 18:40
  • 1
    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. – Jax Cavalera Mar 06 '16 at 10:12
  • 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. – MadPhysicist Jul 30 '17 at 19:03
  • 1
    apparently I did not notice I have bootstrap.js included twice - first time it was way before query – JJ Roman Apr 26 '20 at 20:03
106

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.

yoozer8
  • 6,966
  • 6
  • 47
  • 84
Onur Yıldırım
  • 27,841
  • 11
  • 78
  • 96
  • 3
    ```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? – rikkit Aug 14 '16 at 04:42
  • 1
    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. – Onur Yıldırım Aug 14 '16 at 12:32
  • 1
    You have to install jquery using `npm install jquery` not with bower. – syodage Sep 17 '16 at 19:41
  • 1
    Useful to see how electron deals with the libraries different than a window based app – jwknz Jul 26 '17 at 18:51
  • 1
    I still don't understand it, but Laravel uses the same approach: https://github.com/laravel/laravel/blob/v5.7.0/resources/js/bootstrap.js#L12 – Ryan Nov 07 '18 at 21:33
  • must be multiple things that caused this error because when i added your solkution i got "require is not defined". –  Jan 25 '19 at 17:45
14

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>
124
  • 2,509
  • 22
  • 36
7

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>
Vivek Panday
  • 1,316
  • 2
  • 14
  • 32
4

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>
Mohit Singh
  • 5,163
  • 2
  • 19
  • 24
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.
Ivan
  • 11,733
  • 5
  • 35
  • 63
Karisno1
  • 21
  • 1
1

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>
Ashok kumar
  • 1,322
  • 4
  • 28
  • 55
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.

Ernani
  • 129
  • 1
  • 4
  • 12
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

SantoshK
  • 1,531
  • 13
  • 20
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.

Zarepheth
  • 2,225
  • 2
  • 22
  • 47
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>
Jani Devang
  • 798
  • 9
  • 18
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

Davut Gürbüz
  • 4,462
  • 4
  • 38
  • 73
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

Roshana Pitigala
  • 7,058
  • 8
  • 38
  • 66
kanika
  • 1
0

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
    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"]" – AnaCS Feb 19 '19 at 10:54