6

As I understand, the following commands should install Bootstrap, jQuery, and popper.js:

composer require laravel/ui
php artisan ui bootstrap
npm install && npm run dev

After running these commands I can now use Bootstrap class names, but I cannot use jQuery. When I try to add some jQuery code I get the following error: Uncaught ReferenceError: $ is not defined

I have added <script src="{{ asset('js/app.js') }}"></script> and <link href="{{ asset('css/app.css') }}" rel="stylesheet"> to the head of the document.

Am I missing something here?

Example code:

<!doctype html>
<html class="no-js" lang="da">

<head>
    <script src="{{ asset('js/app.js') }}"></script>
    <link href="{{ asset('css/app.css') }}" rel="stylesheet">
</head>
<body>
<script>
    $(document).ready(function () {
        alert(0);
    });
</script>
</body>
</html>
  • https://stackoverflow.com/questions/47039812/how-to-install-popper-js-with-bootstrap-4 – The Alpha Sep 29 '21 at 22:26
  • Not working as the packages has already been installed automatically. If I try to install jQuery with npm I just get "up to date, audited ......", same goes for popper.js and bootstrap. Also after trying to install them with npm, I ran "npm run dev" again and same problem persist – MichaelJorgensenDK Sep 29 '21 at 22:56

2 Answers2

9

I had a lot of problems with using laravel/ui to install and then use the packages. Also the bootstrap version it installs is outdated as it is only version 4.6, and the newest version is, as of today: 5.1.1 (https://github.com/twbs/bootstrap/releases), so i decided to try install the packages by myself, and finally got everything working.

What I did was to install all the packages with the following commands:

#We do this because it will take care of a lot of things for us
composer require laravel/ui
php artisan ui bootstrap

#And now we upgrade bootstrap and add new popper.js version
npm install bootstrap@latest @popperjs/core --save-dev

This should install all the node_modules including bootstrap v. 5.1.1, jquery v. 3.6, and @popperjs/core v. 2.10.2 and package.json now looks like this:

{
    "private": true,
    "scripts": {
        "dev": "npm run development",
        "development": "mix",
        "watch": "mix watch",
        "watch-poll": "mix watch -- --watch-options-poll=1000",
        "hot": "mix watch --hot",
        "prod": "npm run production",
        "production": "mix --production"
    },
    "devDependencies": {
        "@popperjs/core": "^2.10.2",
        "axios": "^0.21",
        "bootstrap": "^5.1.1",
        "jquery": "^3.6",
        "laravel-mix": "^6.0.6",
        "lodash": "^4.17.19",
        "popper.js": "^1.16.1",
        "postcss": "^8.1.14",
        "sass": "^1.32.11",
        "sass-loader": "^11.0.1"
    }
}

Now in resources/js/bootstrap.js I change the code so it now looks like this:

window._ = require('lodash');

/**
 * We'll load jQuery and the Bootstrap jQuery plugin which provides support
 * for JavaScript based Bootstrap features such as modals and tabs. This
 * code may be modified to fit the specific needs of your application.
 */

try {
    window.$ = window.jQuery = require('jquery');
    window.Popper = require('@popperjs/core');
    window.bootstrap = require('bootstrap');

} catch (e) {}

/**
 * We'll load the axios HTTP library which allows us to easily issue requests
 * to our Laravel back-end. This library automatically handles sending the
 * CSRF token as a header based on the value of the "XSRF" token cookie.
 */

window.axios = require('axios');

window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';

After this change, just run

npm run dev

Now everything works. Here is an example HTML/JS page that puts them all to the test

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>jQuery, popper.js, and Bootstrap</title>
    
    {{--    Load compiled CSS    --}}
    <link rel="stylesheet" href={{ asset('css/app.css') }}>
    
    {{--  popper.js CSS example  --}}
    <style>
        #tooltip {
            background: #333;
            color: white;
            font-weight: bold;
            padding: 4px 8px;
            font-size: 13px;
            border-radius: 4px;
        }
    </style>
</head>
<body>

{{--  Test Bootstrap css  --}}
<div class="alert alert-success mt-5" role="alert">
    Boostrap 5 is working using laravel 8 mix!
</div>

{{--  popper.js HTML example  --}}
<button id="button" aria-describedby="tooltip">My button</button>
<div id="tooltip" role="tooltip">My tooltip</div>

{{--    Load compiled Javascript    --}}
<script src="{{ asset('js/app.js') }}"></script>
<script>
    //Test jQuery
    $(document).ready(function () {
        console.log('jQuery works!');

        //Test bootstrap Javascript
        console.log(bootstrap.Tooltip.VERSION);
    });

    //Test popper.js
    const button = document.querySelector('#button');
    const tooltip = document.querySelector('#tooltip');
    const popperInstance = Popper.createPopper(button, tooltip);
</script>

</body>
</html>
2

Make sure there's not a defer in your <script> tag that imports app.js.

This often causes the $ is not defined error with Laravel Mix.

Leo
  • 21
  • 1