Mateja's avatar

Laravel Mix compiled Bootstrap 4 javascript is not working.

I am trying to compile Bootstrap 4 using Laravel Mix. Sass compiles and works. JavaScript also compiles but Bootstrap Navbar acts like javascript is missing (dropdown menu and hamburger button don't work). Vue on the other hand works. I checked browser console there are no errors. All npm packages are installed.

My resources/js/app.js:

require('./bootstrap');

import Reservations from './Reservations.vue';

new Vue({
  el: '#app',
  components: {
    Reservations
  }
});

My resources/js/bootstrap.js:

window._ = require('lodash');
window.axios = require('axios');
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
window.Vue = require('vue');

window.$ = window.jQuery = require('jquery');
window.Popper = require('popper.js').default;
require('bootstrap');

My webpack.mix.js:

const mix = require('laravel-mix');

mix.js('resources/js/app.js', 'public/js')
  .extract(['lodash', 'axios', 'vue', 'jquery', 'popper.js', 'bootstrap'])
  .sourceMaps()
  .version()
  .sass('resources/sass/app.scss', 'public/css').version();

My package.json:

{
  "private": true,
  "scripts": {
    "dev": "npm run development",
    "development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --config=node_modules/laravel-mix/setup/webpack.config.js",
    "watch": "npm run development -- --watch",
    "watch-poll": "npm run watch -- --watch-poll",
    "hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --disable-host-check --config=node_modules/laravel-mix/setup/webpack.config.js",
    "prod": "npm run production",
    "production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --no-progress --config=node_modules/laravel-mix/setup/webpack.config.js"
  },
  "devDependencies": {
    "axios": "^0.19",
    "cross-env": "^7.0",
    "laravel-mix": "^5.0.1",
    "lodash": "^4.17.19",
    "resolve-url-loader": "^3.1.0",
    "sass": "^1.30.0",
    "sass-loader": "^8.0.2",
    "vue-template-compiler": "^2.6.12",
    "bootstrap": "^4.5.3",
    "jquery": "^3.5.1",
    "popper.js": "^1.16.1",
    "vue": "^2.6.12"
  }
}

My resources/views/welcome.blade.php:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="{{ mix('css/app.css') }}" type="text/css">
    <title>Welcome</title>
  </head>
  <body>
    <nav class="navbar navbar-expand-lg navbar-light bg-light">
      <div class="container-fluid">
        <a class="navbar-brand" href="#">Navbar</a>
        <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarSupportedContent">
          <ul class="navbar-nav me-auto mb-2 mb-lg-0">
            <li class="nav-item">
              <a class="nav-link active" aria-current="page" href="#">Home</a>
            </li>
            <li class="nav-item">
              <a class="nav-link" href="#">Link</a>
            </li>
            <li class="nav-item dropdown">
              <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                Dropdown
              </a>
              <ul class="dropdown-menu" aria-labelledby="navbarDropdown">
                <li><a class="dropdown-item" href="#">Action</a></li>
                <li><a class="dropdown-item" href="#">Another action</a></li>
                <li><hr class="dropdown-divider"></li>
                <li><a class="dropdown-item" href="#">Something else here</a></li>
              </ul>
            </li>
            <li class="nav-item">
              <a class="nav-link disabled" href="#" tabindex="-1" aria-disabled="true">Disabled</a>
            </li>
          </ul>
          <form class="d-flex">
            <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
            <button class="btn btn-outline-success" type="submit">Search</button>
          </form>
        </div>
      </div>
    </nav>
    <div class="container">
      <h1>Reservations: </h1>
      <div id="app">
        <reservations></reservations>
      </div>
    </div>

    <script src="{{ mix('js/manifest.js') }}"></script>
    <script src="{{ mix('js/vendor.js') }}"></script>
    <script src="{{ mix('js/app.js') }}"></script>
  </body>
</html>
0 likes
3 replies
rodrigo.pedra's avatar
Level 56

From a quick glance, change:

  • data-bs-toggle to data-toggle
  • data-bs-target to data-target

References:

https://getbootstrap.com/docs/4.5/components/dropdowns/#single-button

https://getbootstrap.com/docs/4.5/components/navbar/#supported-content

Per your package.json file, you're using bootstrap version 4, so there are no -bs- in those attributes.

Those were added in bootstrap 5, which is in beta now, so Bootstrap docs site defaults to the 5 documentation.

While using Bootstrap 4, be sure to check if you are browsing the corresponding docs to avoid mismatches like this.

As it seems you used the Bootstrap 5 sample code, instead of Bootstrap 4, you might need to change some other classes or attributes. Please compare your code to the docs linked above.

1 like

Please or to participate in this conversation.