In my Laravel project (laravel 5.6) I installed Jquery from npm. Then I added it to webpack.mix.js.
mix.webpackConfig(webpack => {
return { plugins: [new webpack.ProvidePlugin({
$: "jquery",
jQuery: ["jquery", "$"],
"window.jQuery": "jquery",
Popper: ["popper.js", "default"]
})] };
});
after compiling the assets and trying to use jquery it shows
"Uncaught ReferenceError: $ is not defined"
I am using my custom JavaScript file after loading the mix file in my view.
<script src="{{ mix('/js/app.js') }}"></script>
<script type="text/javascript" src="/js/tests/tests.js"></script>
In my custom JavaScript file I added the following code to check Jquery.
$("#myCheckButton").click(function(e) {
console.log(test);
});
I tried changing the webpack.min.js webPackconfig settings but was not able to solve it. Most questions like this recommended to put the custom js files after the mix. I think I got it right in my case
Edit : And there is another error
jquery-jvectormap.min.js?37ce:1 Uncaught TypeError: Cannot read property 'fn' of undefined
at eval (jquery-jvectormap.min.js?37ce:1)
at Object.eval (jquery-jvectormap.min.js?37ce:1)
at eval (jquery-jvectormap.min.js:6)
at Object../node_modules/jvectormap/jquery-jvectormap.min.js (app.js?id=3e76ba2082961d8bb73a:654)
at __webpack_require__ (app.js?id=3e76ba2082961d8bb73a:20)
at eval (index.js:3)
at Object../resources/assets/js/vectorMaps/index.js (app.js?id=3e76ba2082961d8bb73a:1820)
at __webpack_require__ (app.js?id=3e76ba2082961d8bb73a:20)
at eval (bootstrap.js:9)
at Object../resources/assets/js/bootstrap.js (app.js?id=3e76ba2082961d8bb73a:1652)
As requested here is my bootstrap.js file
import './masonry';
import './charts';
import './popover';
import './scrollbar';
import './search';
import './sidebar';
import './skycons';
import './vectorMaps';
import './chat';
import './datatable';
import './datepicker';
import './email';
import './fullcalendar';
import './googleMaps';
import './utils';
import './sweetalert2';
import './select2';
Assuming things worked, then you altered the template, then they didn’t, I’d guess you deleted the code to include the library, or made some other change that means laravel can’t find it. However, according to this SO answer, jQuery should already included in laravel as a dev dependency and you should be able to include it using:
This technique is used on many popular sites, including jquery.com, and solves most [CODE]jQuery is not defined errors [/CODE]. If the CDN jQuery fails load, it will almost certainly load fine from your own domain, but you’ll also see the benefits of using a CDN-hosted jQuery for most of your users.
The most common reason behind the error "Uncaught ReferenceError: $ is not defined" is executing the jQuery code before the jQuery library file has loaded. Therefore make sure that you're executing the jQuery code only after jQuery library file has finished loading. Also if you're including the jQuery library file dynamically, inspect the page ...
The most common reason behind the error "Uncaught ReferenceError: $ is not defined" is executing the jQuery code before the jQuery library file has loaded.
Put this near the top of your resources\assets\js\app.js
file.
import $ from 'jquery';
window.jQuery = $;
window.$ = $;
Source:
https://github.com/webpack/webpack/issues/4258#issuecomment-340240162
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With