86

After import the Bootstrap and Jquery this error is showning when compiling.

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import 'bootstrap/dist/css/bootstrap.css';
import './index.css';

global.jQuery = require('jquery');
require('bootstrap');
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Ranindu
  • 1,013
  • 1
  • 7
  • 13

10 Answers10

228

Popper.js is a dependency of Bootstrap 4 which is used for showing popups. It is a peer dependency of bootstrap meaning it is something that bootstrap requires but doesn't include with itself when installed. So to install popper.js run

npm install popper.js --save

It is setup as a peer dependency because some people just use the css of Bootstrap without the javascript. jQuery and popper.js are the peer dependencies which needs to be installed separately. If you require the javascript of Bootstrap you need to install jQuery and popper.js alongside Bootstrap 4.


Bootstrap 5 requires "Popper.js Core", not Popper.js. You should run this instead:

npm install @popperjs/core --save

(thanks @Kyouma for adding this in the comments)

LuizLoyola
  • 386
  • 3
  • 20
acesmndr
  • 8,137
  • 3
  • 23
  • 28
  • Nice explanation, thanks. Although, wouldn't it be preferable to use `--save-dev`? – JCarlosR Dec 19 '19 at 23:45
  • 2
    @JCarlos It's not technically a dev dependency if it is required for your application to run. So --save makes more sense here. – acesmndr Dec 20 '19 at 04:05
  • thanks for the explanation, although I think it is not a nice example by bootstrap. better would be to split their package then to have js and css seperate, this would be much cleaner – Norman M Oct 18 '20 at 22:43
  • 27
    Just a note - popper.js is now a legacy version. bootstrap 5.x will support `npm i @popperjs/core` instead. The above code (completely correct) is current through v4.x. – Kyouma Apr 08 '21 at 16:39
  • Thanks a bunch. Popper.js Core instead of Popper.js, it's solved – Thang Nguyen Mar 04 '22 at 02:28
  • Interesting that it's only my Windows machine that shows this compile error! No worries on my Mac: Bootstrap 5 and no popper. – redevill Feb 03 '23 at 00:09
23

For bootstrap 5 you are to

npm users: npm install @popperjs/core --save

for yarn users: yarn add @popperjs/core

then import bootstrap like so:


import 'bootstrap/dist/js/bootstrap.bundle';

mimic
  • 4,897
  • 7
  • 54
  • 93
  • Worked for me with Rails 7 esbuild, Stimulus and yarn. No React. How do people figure out how to fix these problems? Thank you – Greg Mar 31 '23 at 21:28
5

It's simple you can Visit this, And save the file as popper.min.js

then import it.

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import 'bootstrap/dist/css/bootstrap.css';
import './index.css';
import 'bootstrap/dist/js/popper.min.js';
global.jQuery = require('jquery');
require('bootstrap');
5

Like tomi0505 have written, you need to import bootstrap and other things like this:

import 'jquery';
import '@popperjs/core'; // Edit here
import 'bootstrap/dist/js/bootstrap.bundle';

After that it will work fine, I hope.

Ramazan
  • 273
  • 3
  • 7
5

I also ran into the same issue and was installing popper using the following command :-

npm install vue-popperjs --save

But, Then I realise I was using bootstrap 5, Which no longer supports this command. So The command used with bootstrap 5 for "Popperjs Core",

npm install @popperjs/core --save
nakli_batman
  • 481
  • 8
  • 4
3

You can direct import like

import bootstrapBundle from './../path to node modules/../node_modules/bootstrap/dist/js/bootstrap.bundle.min.js';

no need to install popper

Daud khan
  • 2,413
  • 2
  • 14
  • 18
2

Add the popper.js as dependency in your package.json file like this:

{
  "dependencies": {
    ...,
    "popper.js": "1.16.1",
    ...,
  }
}

and also add popper.js as required import before adding required bootstrap, as following:

require('popper.js');
require('bootstrap');
technik
  • 1,009
  • 11
  • 17
2

Bootstrap 5 requires "Popper.js Core", You Can run this instead:

npm install @popperjs/core --save
1

if you have installed:
npm install jquery popper.js

instead:
global.jQuery = require('jquery');
require('bootstrap');

add:
import $ from 'jquery';
import Popper from 'popper.js';
import 'bootstrap/dist/js/bootstrap.bundle.min';

look this

tomi0505
  • 21
  • 1
1
npm i bootstrap@4.6.2

this code in the best solution. because bootstrap use popper in this version separately.