question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

jQuery autoloading apparently not working

See original GitHub issue
  • Laravel Mix Version: 0.9.2
  • Node Version (node -v): v6.9.5
  • NPM Version (npm -v): 3.10.10
  • OS: VM Homestead

Description:

I can’t load jQuery using laravel-mix. I’ve added this snippet to webpack.mix.js :

mix.autoload({
   jquery: ['$', 'window.jQuery']
});

as shown here: https://github.com/JeffreyWay/laravel-mix/blob/master/docs/autoloading.md

In any blade template, I can add something like this

<script>
    $(document).ready(function () {
        alert("jQuery loaded");
    });
</script>

But the $ is not recognised (browser error: ReferenceError: Can't find variable: $)

In case it was an issue with the setup of my project, I’ve created a new laravel project from scratch, and I have the same issue.

Steps To Reproduce:

laravel new testsite edit welcome.blade.php and add the script snippet mentioned above npm run dev open testsite, browser shows error mentioned above

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:15

github_iconTop GitHub Comments

6reactions
MladenJanjetoviccommented, Apr 18, 2018

Be careful with script defer attribute also. That will make your autoloaded jQuery unavailable in blade files

6reactions
jnanincommented, Apr 14, 2017

@MladenJanjetovic the whole setup seems confusing to me as a Laravel newcomer, but the way I finally made it work correctly was:

  1. Loading my external js files that need JQuery with Mix (file webpack.mix.js). Example:
mix.autoload({
  jquery: ['$', 'window.jQuery', 'jQuery'],
  moment: 'moment'
});

mix.js(['resources/assets/js/app.js', 'a-module-path/a-jquery-dependant-script.js'], 'public/js')
   .sass('resources/assets/sass/app.scss', 'public/css');

That way, $ is available later on when you require it in a blade template.

Read more comments on GitHub >

github_iconTop Results From Across the Web

jQuery autoloading apparently not working · Issue #646 - GitHub
In my project, the issue comes from the loading order: I load <script src="/js/app.js"></script> after yielding the content - @yield('content') ...
Read more >
Does laravel 6 auto-load jquery? Getting uncaught type error
So I don't know what's going on. Theoretically it's being double loaded but if i comment it out, it's obviously not loaded from...
Read more >
jQuery UI libraries are loading too much CSS that needs to be ...
The problem is that we auto-load the default theme of jQuery UI, which is then applied to all elements using jQuery UI's CSS...
Read more >
Lightbox is causing other jQuery to malfunction - SitePoint
If that doesn't work, try running jQuery in compatibility mode. Prototype overwrites the $ function, which causes problems when using both ...
Read more >
Other events not working after load - jQuery Forum
Hello, I am trying to add a div to my container on click and inside the div - I have tabs and other...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found