I am trying to install jquery in my laravel project with npm i jquery. then, I run npm run dev. But, when i add jquery code to my blade files, it does not work. It always throws the error in console '$ is not a function'.
If, I try to add jquery using CDN url, it works. what could be the reason that 'cdn url' works and npm scaffolding does not work.
You don't just install jQuery using npm
and expect it work out of thin air, you also need to include it into your resources/js/app.js
file or whatever it's called and add that file to your blade template:
<script src="{{ asset(mix('js/app.js')) }}" defer></script>
After you've done this, you pick what you want to do next. Either add inline JS below the included script or add your code inside app.js
or in a separate file.
@geowrgetudor , thank you for responding. I have already included jquery in app.js like below
window.$ = require('jquery');
then, I run 'npm run dev' and put the app.js file in my layout file but still it's not working. below is the git repo where i have used this.
Does any of the following work in your app.js
? Do they output test
in the browser's console?
require('./bootstrap');
// case 1
$(document).ready(function() {
console.log("test");
});
// case 2
jQuery(document).ready(function($) {
console.log("test");
});
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community