Search code examples
jquerysymfonywebpack-encore

jQuery is not defined on my webpack encore


On my app.js i have

import $ from 'jquery';
global.$ = global.jQuery = $;
import 'chart.js/dist/Chart.min';
import 'chartjs-plugin-labels/build/chartjs-plugin-labels.min';
import 'bootstrap/js/dist/dropdown';
import '../../vendor/snapappointments/bootstrap-select/dist/js/bootstrap-select.min';
import 'ajax-bootstrap-select/dist/js/ajax-bootstrap-select.min';
import Lightpick from 'lightpick';

This is my webpack.confing-js

    .setOutputPath('public/build/')
    .setPublicPath('/build')
    .enableVersioning()
    .addEntry('app', './assets/js/app.js')
    .splitEntryChunks()
    .enableSingleRuntimeChunk()
    .cleanupOutputBeforeBuild()
    .enableBuildNotifications()
   .autoProvidejQuery()

Error... can't get it working. tried many solutions but nothing

ajax-bootstrap-select.min.js:21 Uncaught ReferenceError: jQuery is not defined
    at Object../node_modules/ajax-bootstrap-select/dist/js/ajax-bootstrap-select.min.js (ajax-bootstrap-select.min.js:21)

Solution

  • I had exactly the same problem with Bootstrap and JQuery.

    I write you my solution and I hope it will be useful:

    /** app.js **/
    //.. Here I include the app.scss
    
    //import $ from 'jquery' DOESN'T WORK
    const $ = require('jquery');
    Window.prototype.$ = $; //HERE IS MY SOLUTION (Without this line it doesn't work!)
    
    //Here I declare bootstrap
    require('bootstrap');
    //..
    // I include some library which need JQuery
    require('bootstrap/js/dist/tooltip');
    require('bootstrap/js/dist/popover');
    
    import bsCustomFileInput from "bs-custom-file-input";
    
    //Now $ is working!
    $(document).ready(function () {
        $('[data-toggle="popover"]').popover();
        $('[data-toggle="tooltip"]').tooltip();
        $('[data-toggle=offcanvas]').click(function () {
            $('.row-offcanvas').toggleClass('active');
        });
    });
    

    In my webpack.config.js I do NOT uncomment these lines

    
        // uncomment if you're having problems with a jQuery plugin
        //.autoProvidejQuery()