Having trouble using Popper.js with Bootstrap 4 Beta in Webpack 3.x? You might encounter the error message "Popper is

Exciting news - Bootstrap 4 Beta has been released! However, Tether has been replaced with Popper.js for tooltip functionality (among other features). This change has triggered a new error in the console, prompting me to switch to Popper.js:

Bootstrap dropdown require Popper.js

Updating my webpack.config.js (you can view the full config here) seemed to resolve the issue (simply replacing Tether with Popper):

plugins: [
new ProvidePlugin({
  'Promise': 'bluebird',
  '$': 'jquery',
  'jQuery': 'jquery',
  'window.jQuery': 'jquery',
  'window.$': 'jquery',
  Popper: 'popper.js' 
}),

I also added import 'bootstrap' to my main.ts file.

However, a new problem has arisen (not present with Tether) - a console error now states:

Uncaught TypeError: Popper is not a constructor

Upon debugging in Chrome, I noticed that Popper is loaded as an Object (resolving the Bootstrap complaint) as shown in the screenshot below. https://i.sstatic.net/pyLZi.jpg

In conclusion, I utilize Bootstrap tooltip within a basic custom element constructed with Aurelia and TypeScript (previously functional with Bootstrap alpha 6 and Tether).

import {inject, customAttribute} from 'aurelia-framework';
import * as $ from 'jquery';

@customAttribute('bootstrap-tooltip')
@inject(Element)
export class BootstrapTooltip {
  element: HTMLElement;

  constructor(element: HTMLElement) {
    this.element = element;
  }

  bind() {
    $(this.element).tooltip();
  }

  unbind() {
    $(this.element).tooltip('dispose');
  }
}

It seems that my Popper import may have been incorrect. My question is, what is the recommended method to properly import Popper using Webpack 3.x?

Answer №1

During my exploration of the Bootstrap 4 documentation, I stumbled upon a helpful section regarding Webpack. This section provides clear instructions on how to correctly install it. By following the steps outlined in the Bootstrap - installing with Webpack guide, the solution is as simple as making modifications to the webpack.config.js file:

plugins: [
  // ...
  new webpack.ProvidePlugin({
    $: 'jquery',
    jQuery: 'jquery',
    'window.jQuery': 'jquery',
    Popper: ['popper.js', 'default']
  })
  // ...
]

Additionally, don't forget to import it in the main.ts file:

import 'bootstrap';

Following these steps, we can quickly get back to business with Bootstrap 4 in no time! :)

Answer №2

For those utilizing Webpack, follow these steps:

window.$ = window.jQuery = require('jquery');
window.Popper = require('popper.js').default; // Make sure to include "default"
require('bootstrap/dist/js/bootstrap');

Answer №3

When using bootstrap": "^4.1.1", there is no need to specifically import jquery and popper.js. These plugins are already included when you import 'bootstrap' or its individual plugins.

Note: If you decide to import plugins individually, you must also install exports-loader.

You do not have to require files with

require('exports-loader?file ... ')
as detailed here, because this is automatically handled by simply installing
$ npm install exports-loader --save-dev
.

import 'bootstrap'; // Import all plugins at once

//
// Or, import plugins individually
//
// import 'bootstrap/js/src/alert';
// import 'bootstrap/js/src/button';
// import 'bootstrap/js/src/carousel';
// import 'bootstrap/js/src/collapse';
// import 'bootstrap/js/src/dropdown';
// import 'bootstrap/js/src/modal';
// import 'bootstrap/js/src/popover';
// import 'bootstrap/js/src/scrollspy';
// import 'bootstrap/js/src/tab';
// import 'bootstrap/js/src/tooltip';
// import 'bootstrap/js/src/util';

There is no need for configuration like the following:

const webpack = require('webpack');

module.exports = {
  configureWebpack: {
    plugins: [
      new webpack.ProvidePlugin({
        $: 'jquery',
        jQuery: 'jquery',
        'window.jQuery': 'jquery',
        Popper: ['popper.js', 'default']
      })
    ]
  }
}

As a vue.js developer, in the new vue-cli-3, we create vue.config.js in the root and include code like the one mentioned above to register a new plugin. However, in bootstrap": "^4.1.1", all this is unnecessary.

Bootstrap's tooltip plugin is dependent on popper.js and needs to be activated manually. You can achieve this by following the example below in the component where you are using the tooltip element:

<script>
  import $ from 'jquery';

  export default {
    mounted() {
      $('[data-toggle="tooltip"]').tooltip();
    },
  };
</script>

Answer №4

Encountered the same issue recently and found the solution detailed in this link: https://github.com/FezVrasta/popper.js/issues/287

After making changes, my main.ts now resembles something like this:

import "jquery";
import Popper from "popper.js";

(<any>window).Popper = Popper;

require("bootstrap");

To ensure the require call works, I had to execute

npm install @types/requirejs --save
.

Update: A more efficient solution can be found in the documentation https://getbootstrap.com/docs/4.0/getting-started/webpack/

plugins: [
  ...
  new webpack.ProvidePlugin({
    $: 'jquery',
    jQuery: 'jquery',
    'window.jQuery': 'jquery',
    Popper: ['popper.js', 'default'],
    // If plugins were imported individually, they must be required here:
    Util: "exports-loader?Util!bootstrap/js/dist/util",
    Dropdown: "exports-loader?Dropdown!bootstrap/js/dist/dropdown",
    ...
  })
  ...
]

Answer №5

To incorporate essential scripts into an ASP.net Core 2 project, simply include the following lines of code in the main HTML file ("_Layout.cshtml"):

<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/js/popper.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.min.js"></script>

This approach has proven successful for me.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unable to choose radio button again

Check out this fun web quiz I created! I'm having some trouble with the back button functionality. Whenever a user clicks back, I want to restore their previous choice but for some reason it's not working as expected. Take a look at my go_back ...

What's the secret behind generating a crisp 400 on paper?

Understanding Why it Prints 400 I'm struggling to comprehend the logic behind this var x = {}, y = { key: "y" }, z = { key: "z" }; x[y] = 100; x[z] = 200; console.log(x[y] + x[z]); ...

Show different options like selection menus, checkboxes, date pickers, and more based on the content of a JSON

Looking for Help with Dynamic Input Fields and Additional Elements I have successfully implemented dynamic input fields based on a JSON file, but now I need to include selections, checkboxes, and a datepicker as well according to their respective groups. ...

What is the best way to capture a screenshot using selenium in conjunction with synchronous JavaScript?

Currently, I am developing an automated test using javaScript and leveraging a node library called webdriver-sync. This library simplifies writing selenium tests by eliminating the need for callbacks and promises, and it utilizes the java Webdriver API. Su ...

Can one extract request data from an rxjs-timeout-error?

Currently, I am working on enhancing our error handling system, particularly in providing better descriptions of errors in both general and testing environments. My focus is on an Ionic app, but I am facing challenges with the rxjs timeout method. One asp ...

Content not appearing in ng repeat loop

I'm facing a basic issue that I can't seem to solve - my code isn't working as expected: <article id="desktop"> <h3>Content: </h3> <ul> <li ng-repeat="x in storage"> name: {{x.name}} ...

What is the best way to create a repetitive pattern using a for loop in Javascript?

I want to create a repeating pattern to color the background, but once i exceeds colors.length, the background color stops changing because, for example, colors[3] does not exist. I need colors[i] to start back at 0 until the first loop is complete. cons ...

Trouble with displaying canvas images in imageDraw() function

I'm having trouble with my Imagedraw() method when trying to obtain image data using toDataURL(). var video = document.getElementById('media-video'); var videoCurrentTime = document.getElementById('media-video').currentTime; var ...

JavaScript can dynamically attach EventListeners to elements, allowing for versatile and customized event

I am currently populating a table using data from an XML file. One of the columns in the table contains links to more details. Due to the unique requirements of my web page setup (Chrome extension), I need to dynamically add an event handler when the table ...

Configure environment files for different environments in React using webpack: .env, .env.test, and .env.production

Here is the package.json setup for my React app: "scripts": { "dev": "webpack-dev-server --mode=development", "prod": "webpack --mode=production" } I am looking to set environment variables in .env, .env.t ...

Manipulate the value of the <input> element when focused through JavaScript

After I focus on the input field, I was expecting to see Bond-Patterson, but instead, I am only getting Bond. What could be causing this discrepancy and how can it be fixed? $('input[name="surname"]').attr("onfocus", "this.placeholder='Bo ...

Confirm the session validity before invoking the web service function from an ajax function triggered by clicking a button

I am experiencing a complex issue. On a page, users input data and then click on a button to save all the parameters into a JSON object. After saving locally, they click another button which triggers an ajax method that calls a web service method on the sa ...

Tips for adjusting the position of an icon when encountering a line break using Javascript or CSS

After some trial and error, I managed to get it working, but only when the page is initially loaded and not in a responsive manner. Below is the JavaScript code I used. if ( $(".alert-box").height() >= 90 ) { $('.img').css(&apos ...

Enhance your JavaScript skills by deserializing objects and seamlessly integrating new methods

Currently in my Javascript code, I am utilizing localStorage. Since objects cannot be directly stored in it, I am using JSON.stringify to serialize them before saving. Within localStorage, I am storing the entire game state, where some of the sub-objects ...

Eliminating the glow effect, border, and both vertical and horizontal scrollbars from a textarea

Dealing with the textarea element has been a struggle for me. Despite adding decorations, I am still facing issues with it. The glow and border just won't disappear, which is quite frustrating. Could it be because of the form-control class? When I rem ...

Error: The system encountered an issue while trying to access an undefined property 'find'

I've been working on developing the backend for a wishlist feature, but I've encountered an issue with my code. (node:19677) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'find' of undefined Despite trying to redefi ...

Are there any methods to implement object-oriented programming in JavaScript?

The concept of prototype-based object-oriented programming in JavaScript is intriguing, but there are many scenarios where the need for class-based object creation arises. Consider a vector drawing application, where the workspace begins empty and you can ...

Is there a way to prevent a link from activating when I click on one of its internal elements?

Within a div nested inside an "a" tag (specifically in Link within next.js), there is another div labeled as "like." When clicking anywhere within the main div, the intention is for it to redirect to the destination specified by the "a" tag. However, if th ...

Prevent the bootstrap collapse hide feature from functioning when content is already visible or expanded

I have a simple question. I would like to utilize the Bootstrap 3 collapse function to display some content, but without the ability to hide it when clicking on the link or button. Basically, I want the content to appear when clicking on the link or butto ...

The Facebook share button on my website is malfunctioning

Could someone please help me figure out why my custom Facebook share button is not functioning properly? When I click the button, the share window fails to appear. I have tested it as an html file on a live web server and have thoroughly reviewed the Faceb ...