Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap 5 ReferenceError: bootstrap is not defined

I'm just finish install bootstrap 5 version ^5.0.0-alpha1 and import bootstrap in app.js

import "bootstrap"

other.js

var myModal = new bootstrap.Modal(document.getElementById('myModal'));
myModal.show();

when I run the code it give me error ReferenceError: bootstrap is not defined.

then I try to import bootstrap in other.js like this

import bootstrap from "bootstrap"

but when I run npm run dev error "export 'default' (imported as 'bootstrap') was not found in 'bootstrap'

If Im import modal manually like import { Modal } from "bootstrap" it give me error "TypeError: Illegal invocation"

like image 291
Rimuru Tempest Avatar asked Sep 29 '20 05:09

Rimuru Tempest


People also ask

Do I need jQuery for bootstrap 5?

Bootstrap 5 is designed to be used without jQuery, but it's still possible to use our components with jQuery. If Bootstrap detects jQuery in the window object it'll add all of our components in jQuery's plugin system; this means you'll be able to do $('[data-bs-toggle="tooltip"]').

Does bootstrap 5 need Popper JS?

Many of our components require the use of JavaScript to function. Specifically, they require our own JavaScript plugins and Popper.

Does bootstrap require JavaScript?

Many of our components require the use of JavaScript to function. Specifically, they require jQuery, Popper. js, and our own JavaScript plugins.

Does bootstrap come with jQuery?

Bootstrap uses jQuery for JavaScript plugins (like modals, tooltips, etc). However, if you just use the CSS part of Bootstrap, you don't need jQuery.


Video Answer


3 Answers

If anyone trying to use with webpack, this solution worked for me.

window.bootstrap = require('bootstrap/dist/js/bootstrap.bundle.js');
like image 191
Alaksandar Jesus Gene Avatar answered Oct 11 '22 02:10

Alaksandar Jesus Gene


If you use:

    import { Modal } from 'bootstrap'

then the syntax to use will be:

    let myModal = new Modal(document.getElementById('myModal'));
    myModal.show();

Working for me on a relatively simple setup with Vite and bootstrap-5.0.0-beta1.

like image 35
kaliatech Avatar answered Oct 11 '22 02:10

kaliatech


Bootstrap 5.1 + webpack 5:

app.js

import * as bootstrap from 'bootstrap';
window.bootstrap = bootstrap;

or if you only need Modal:

import * as bootstrap from 'bootstrap';
window.Modal = bootstrap.Modal;

and then you could use in other.js like:

var myModal = new Modal(document.getElementById('myModal'));
myModal.show();
like image 5
Patryk Chowratowicz 'Zszywacz' Avatar answered Oct 11 '22 04:10

Patryk Chowratowicz 'Zszywacz'