wambaozhixing's avatar

Laravel Mix - Required jQuery plugins can't be found

I attempting to use require to include some Javascript plugins for my project, but for some reason, the plugins simply can't be found by the browser.

My resources/assets/js/bootstrap.js and resources/assets/js/app.js files when "compiled" with npm run dev include all the scripts I need, but when running the project in the browser, it simply wont work.

These are my bootstrap.js and app.js:

window._ = require('lodash');

/**
 * We'll load jQuery and the Bootstrap jQuery plugin which provides support
 * for JavaScript based Bootstrap features such as modals and tabs. This
 * code may be modified to fit the specific needs of your application.
 */

window.$ = window.jQuery = require('jquery');

require('bootstrap-sass');

/**
 * Vue is a modern JavaScript library for building interactive web interfaces
 * using reactive data binding and reusable components. Vue's API is clean
 * and simple, leaving you to focus on building your next great project.
 */

window.Vue = require('vue');

/**
 * We'll load the axios HTTP library which allows us to easily issue requests
 * to our Laravel back-end. This library automatically handles sending the
 * CSRF token as a header based on the value of the "XSRF" token cookie.
 */

window.axios = require('axios');

window.axios.defaults.headers.common = {
    'X-CSRF-TOKEN': window.Laravel.csrfToken,
    'X-Requested-With': 'XMLHttpRequest'
};

/**
 * Let's load Full Calendar
 */
require('fullcalendar');
require('./bootstrap');
require('./bootstrap-checkbox-radio-switch')
require('./chartist.min')
require('./bootstrap-notify')
require('./bootstrap-select')
require('./maps')
require('./demo')

Fr example, when I use fullCalendar, I get the following error:

Uncaught TypeError: $(...).fullCalendar is not a function

I simply have no idea how to make these plugins take effect.

Help would be appreciated.

0 likes
17 replies
lara25260's avatar

I posted similar question, if only people would response :/

JanBabinec's avatar

Hi, its easy, Laravel Mix ships with jQuery autoloading out of the box. All what you need is call mix.autoload before you try to compile other JavaScript files. for ex. mix.autoload({ 'jquery': ['window.$', 'window.jQuery'] }); mix.js('resources/assets/js/app.js', 'public/js') .sass('resources/assets/sass/app.scss', 'public/css');

1 like
alenabdula's avatar

_bootstrap.js needs to be required before require('fullcalendar');

wambaozhixing's avatar

@hendranucleo I checked the post, but I'm already using require('fullcalendar'), but unsuccessfully. Any other ideas, anyone?

hendranucleo's avatar

@dugajean, Sorry, this is via npm install or file download? if you using *vue aka vue component, require the fullcalendar before export.

hendranucleo's avatar

At my end i have no problem load fullcalendar. With very minimal setup.

bootstrap.js

window.$ = window.jQuery = require('jquery');
window.Pace = require('node-pace-progress');
/**
 * We'll load the axios HTTP library which allows us to easily issue requests
 * to our Laravel back-end. This library automatically handles sending the
 * CSRF token as a header based on the value of the "XSRF" token cookie.
 */

require('fullcalendar')

window.axios = require('axios');

window.axios.defaults.headers.common = {
    'X-CSRF-TOKEN': window.fyi.csrfToken,
    'X-Requested-With': 'XMLHttpRequest'
};

/**
 * Echo exposes an expressive API for subscribing to channels and listening
 * for events that are broadcast by Laravel. Echo and event broadcasting
 * allows your team to easily build robust real-time web applications.
 */

// import Echo from "laravel-echo"

// window.Echo = new Echo({
//     broadcaster: 'pusher',
//     key: 'your-pusher-key'
// });

$('#xxx').fullCalendar();

Laravel Blade

<div id="xxx"></div> 

test.scss

@import "../../../../node_modules/fullcalendar/dist/fullcalendar.css";

Note, even i didnt load moment.js as per instruction it still working.

http://i.imgsafe.org/9cb5129be4.png

Check your script orders, maybe you call $('#some_el').fullCalendar() before you load fullCalendar it self.

1 like
robgoodliffe's avatar

Not sure if this will help but I was having some problems getting the bootstrap jQuery plugins to work in 5.4 when upgrading a project.

adding mix.autoload({}); before everything else in the webpack.mix.js file seems to have solved the issue for me.

xinkenan11's avatar

replace window.$ to global.$ in app.js /bootstrap.js etc //window.$ = window.jQuery = require('jquery') global.$ = global.jQuery = require('jquery')

lukag's avatar

For all the future googlers, I solved the problem with

mix.autoload({
    jquery: ['$', 'window.jQuery',"jQuery","window.$","jquery","window.jquery"]
});
4 likes
itsali's avatar

I tried whatever mentioned I still have the problem. I noticed php storm specify an error with const { mix } = require('laravel-mix'); line and told my version of javascript does not support this

meigire's avatar

I solved this with:

mix.autoload({
    jquery: ['$', 'global.jQuery',"jQuery","global.$","jquery","global.jquery"]
});
1 like

Please or to participate in this conversation.