Rails 6 + Webpacker: Uncaught TypeError: $(...).select2 is not a function

Viewed 1099

I'm trying to get select2 working in my app, but couldn't able to. I've added it by yarn add select2. Below is my code

javascript/packs/application.js

// This file is automatically compiled by Webpack, along with any other files
// present in this directory. You're encouraged to place your actual application logic in
// a relevant structure within app/javascript and only use these pack files to reference
// that code so it'll be compiled.

require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")
require("jquery")
require("bootstrap")
require("tempusdominus-bootstrap-4")
require("moment/locale/ja")
require('select2')


// stylesheets
require("../stylesheets/application.scss")

import "@fortawesome/fontawesome-free/css/all.css";
import "./owl.carousel.min.js";
import 'select2';
import 'select2/dist/css/select2.css';

$(document).ready(function(){ 
    $('.customers-dropdown').select2();
    $("select").change(function(event) {
        $("#extra_price").html(formatCurrency($("#extra_" + $(".select").val()).html()));
        computetotal(event); 
    });

    $('#inlineDatepicker').datetimepicker({
        inline: true,
        sideBySide: false,
        format: 'L',
        useCurrent: false,
        defaultDate: $('#weekStardate').val(),
    
    });

    $("#inlineDatepicker").on("change.datetimepicker", ({date, oldDate}) => {   
        var fdate = moment(date)        
        $('#weekStardate').val(fdate.format("D/M/YYYY")) 
     })
        
}); 

package.json

{
  "name": "rails_6_devise_example",
  "private": true,
  "dependencies": {
    "@fortawesome/fontawesome-free": "^5.14.0",
    "@rails/actioncable": "^6.0.0-alpha",
    "@rails/activestorage": "^6.0.0-alpha",
    "@rails/ujs": "^6.0.0-alpha",
    "@rails/webpacker": "^5.2.1",
    "bootstrap": "^4.5.2",
    "bootstrap-datepicker": "^1.9.0",
    "jquery": "^3.5.1",
    "moment": "^2.27.0",
    "node-sass": "^4.12",
    "popper.js": "^1.16.1",
    "rails-ujs": "^5.2.4-3",
    "select2": "^4.1.0-rc.0",
    "tempusdominus-bootstrap-4": "^5.1.2",
    "turbolinks": "^5.2.0"
  },
  "version": "0.1.0",
  "devDependencies": {
    "webpack-dev-server": "^3.8.1"
  }
}

I get Uncaught TypeError: $(...).select2 is not a function. I've looked into many posts and as suggested, I tried to add import $ from 'jquery'; above import 'select2'; which works but that breaks datetimepicker resulting in Uncaught TypeError: jquery__WEBPACK_IMPORTED_MODULE_2___default(...)(...).datetimepicker is not a function I don't have a clue what is happening.

Any help is much appreciated, thanks!

1 Answers

I prepared the code quickly and it works fine. I just added this to the plugins:

new webpack.ProvidePlugin({
  $: 'jquery',
  jQuery: 'jquery',
  'window.jQuery': 'jquery',
  moment: 'moment'
})

I also changed the order, because that is what the turbolink wanted ;)

require("jquery")
require("bootstrap")
require("tempusdominus-bootstrap-4")
require("moment/locale/ja")
require("turbolinks").start()
require('select2')

enter image description here

Of course there are some bugs but I miss formatCurrency but the most important things work :)

Related