Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add jQuery on laravel 5.4 mix

I'm already familiar with laravel 5.1 mix (elixir), and recently I decided to test laravel 5.4 mix.

I mixed my libs on vendors files.

mix
.styles([
    './node_modules/bootstrap/dist/css/bootstrap.css',
    './node_modules/font-awesome/css/font-awesome.css'
], 'public/css/vendor.css')
.js([
    './node_modules/jquery/dist/jquery.js',
    './node_modules/bootstrap/dist/js/bootstrap.js',
], 'public/js/vendor.js')
.disableNotifications()
.version();

And included vendor on my page.

<script src="{{ mix('js/vendor.js') }}" type="text/javascript" charset="utf-8" async defer></script>

But, when I want use jQuery I have this error on image below. Before, in the laravel 5.1 I did exactly that way.

enter image description here

What do I have to do?

like image 906
Felipe Mathais Avatar asked Mar 06 '17 17:03

Felipe Mathais


People also ask

Can I use jquery with laravel?

There is three way you can use jquery in laravel 9 you can use cdn or download jquery and import in laravel project or you can use jquery via npm.

Does laravel 8 come with jquery?

jQuery is already included in this version of laravel as a dev dependency. This first command will install the dependencies. jquery will be added.

WHAT IS MIX () in laravel?

Laravel Mix, a package developed by Laracasts creator Jeffrey Way, provides a fluent API for defining webpack build steps for your Laravel application using several common CSS and JavaScript pre-processors. In other words, Mix makes it a cinch to compile and minify your application's CSS and JavaScript files.

Where is webpack mix js in laravel?

It should be in the root folder of your project, look at the laravel repo here.


1 Answers

Just uncomment this line in your assets/js/bootstrap.js file:

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

And make sure to require the bootstrap file in your assets/js/app.js like this:

require('./bootstrap');
like image 71
Gonzalo Faus Avatar answered Sep 28 '22 16:09

Gonzalo Faus