5

I have a Rails 6 application and assets are compiled using Webpack 4.39.1 (Webpacker gem) .

I have added JQuery 3.4.1 and Popper.js to webpack ProvidePlugin as follows (config/webpack/environment.js):

const { environment } = require('@rails/webpacker');

const webpack = require('webpack');

environment.plugins.append('Provide', new webpack.ProvidePlugin({
    $: 'jquery/src/jquery',
    jQuery: 'jquery/src/jquery',
    Popper: ['popper.js', 'default']
}));

module.exports = environment;

I have a javascript/packs/application.js file and it is the entry point for webpack.

I have added Bootstrap 4.1.1 and importing it to application.js.

Now, I'm trying to setup a tooltip as follows:

import "bootstrap/dist/js/bootstrap";

$( document ).ready(function() {
    $('[data-toggle="tooltip"]').tooltip({
        container: 'body',
        placement: 'auto'
    });
});

I am getting Uncaught TypeError: $(...).tooltip is not a function error in dev console.

I also checked $.fn and found none of the Bootstrap JS methods are part of JQuery prototype.

Any help would be appreciated! Thanks

Tony Ngo
  • 19,166
  • 4
  • 38
  • 60
dp7
  • 6,651
  • 1
  • 18
  • 37

2 Answers2

0

Add this to your webpack. Also install poper.js too

npm i popper.js

In your webpack

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

If you using ts add this in your typing.d.ts.

I'm also facing this same issue. After that to be sure restart IDE and your webpack again.

interface JQuery<any> {
    tooltip(params: any): any;
}
Tony Ngo
  • 19,166
  • 4
  • 38
  • 60
  • Have you fixed this issue with vanilla js ? I am not sure where to place this `typing.d.ts.` in a Rails - Webpacker setup. – dp7 Sep 28 '19 at 12:51
  • Is that an acceptable answer? I'm struggling with "$ is not a function" error on Chrome console and not sure how to fix that. – Askar Hussain Jan 17 '20 at 01:31
  • 1
    @AskarHussain you can try with my answer or https://stackoverflow.com/questions/56941924/angular-8-using-jquery/56944859#56944859 – Tony Ngo Jan 17 '20 at 02:57
0

You can solve this by adding to your gemfile 'jquery-ui-rails' and 'jquery-ui-themes' Others have solved it by using import("bootstrap") in application.js

Mices
  • 89
  • 3
  • 9