Home > Back-end >  ReferenceError $ not defined in laravel
ReferenceError $ not defined in laravel

Time:06-27

I installed a fresh copy of Laravel 8 but I am unable to use $ in jQuery. I know that this is a frequent question here in SO link this but I am unable to jQuery in document ready.

Here are the steps that I have tried based on what I read in various SO and blogs:

npm install jquery --save-dev (someone said that jquery was removed from laravel 6 in this)

npm install jquery-ui --save-dev

added in main.blade.php (under /body)

<script src="{{ asset('js/app.js') }}"></script>

in bootstrap.js

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

in app.scss

@import "~jquery-ui/themes/base/all.css";

webpack.mix.js

mix.js("resources/js/app.js", "public/js")
    .sass("resources/sass/app.scss", "public/css")
    .sass("resources/scss/normalize.scss", "public/css")
    .sass("resources/scss/main.scss", "public/css")
    .sourceMaps();

run npm run dev

I know this is a very basic question but I don't know how to proceed.

EDIT I got it working. I added <script src="{{ asset('js/app.js') }}"></script> under </body> it should have been inside <head></head>. The problem was when the app.js should have been declared.

CodePudding user response:

Kenneth, Please put this line at starting of 'resources/js/app.js' instead of bootstrap.js

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

CodePudding user response:

Please add any jquery CDN inside the file that have this error

  • Related