Laravel-mix: mix.js broke some jquery function inside it

Created on 14 Feb 2017  路  3Comments  路  Source: JeffreyWay/laravel-mix

  • Laravel Mix Version: 0.7.4
  • Node Version (node -v): 7.5.0
  • NPM Version (npm -v): 4.1.2
  • OS: Windows 10 x64

I'm using Laravel Mix (0.7.4) to compile down my JS file (contain datatable).

$('.table').DataTable({
    columnDefs: [
        {targets: 0, orderable: false, searchable: false},
    ]
});

But every time I run it the console always return me Uncaught TypeError: $(...).DataTable is not a function

at my index.blade.php

 <script src="{{ asset('vendor/jquery/dist/jquery.js') }}"></script>
 <script src="{{ asset('vendor/datatables/media/js/jquery.dataTables.js') }}"></script>

webpack.mix.js

mix.js(`resources/assets/js/pages/city.js`, 'public/js/admin/pages/city.js');

When I use Laravel Elixir webpack function working normally, but when I change to Laravel Mix it become error.

Before I init those 2 file seperately I have mix version (mix.combine) of jquery and jquery.datatables. But its not working.

To make it work I need to put $('.table').DataTable() outside any JS. I think something broke it when I run mix.js()

Any solution?

Most helpful comment

You can try setting mix.autoload({}) in your webpack.mix.js file.

@jwhulette - I just pushed a commit that disables rewriting window.jQuery, so that might fix your particular issue. f83b54fca8976a770f51edd2e995048f17337418

Otherwise, if someone can give me reproducible steps in a brand new project, that would be appreciated.

All 3 comments

+1

I'm using AdminLTE, which comes with many jquery plugins
If I "mix.js" the main app.js file, I get an error "$(...).tooltip is not defined"
If I don't mix the app.js, everyhing's fine.

My guess is the following: Laravel Mix creates a local copy of jquey, discarding all the registered plugins.

I had the same problem using SB Admin 2. In bootstrap.js I changed:
// window.$ = window.jQuery = require('jquery');
window.$ = global.jQuery = require('jquery');

You can try setting mix.autoload({}) in your webpack.mix.js file.

@jwhulette - I just pushed a commit that disables rewriting window.jQuery, so that might fix your particular issue. f83b54fca8976a770f51edd2e995048f17337418

Otherwise, if someone can give me reproducible steps in a brand new project, that would be appreciated.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rderimay picture rderimay  路  3Comments

terion-name picture terion-name  路  3Comments

tillkruss picture tillkruss  路  3Comments

mementoneli picture mementoneli  路  3Comments

dtheb picture dtheb  路  3Comments