Chris1904's avatar

$(…).DataTable is not a function when using Laravel Mix

Hi everyone

I struggle using Laravel Mix and DataTables. The issue I have is that when I compile down my .js-files etc., each time I would then visit a page that would execute a jQuery datatable, the following error is thrown:

The error is:

    jQuery.Deferred exception: $(...).DataTable is not a function TypeError: $(...).DataTable is not a function
    Uncaught TypeError: $(...).DataTable is not a function

From what I understand, $(...).DataTable is not a global variable, but how can I make sure that it is accessible "on a global scope" / within my app?

The following is my setup:

app.js

    import jquery from 'jquery/dist/jquery.slim'
    import 'bootstrap-sass'
    import 'datatables.net';
    import dt from 'datatables.net-bs';
    
    window.$ = window.jQuery = jquery;

webpack.mix.js

    mix
        .js('resources/assets/admin/js/app.js', 'js/')
        .extract([
            'jquery', 'bootstrap-sass', 'datatables.net', 'datatables.net-bs'
        ])
        .autoload({
            jquery: ['$', 'window.jQuery', 'jQuery', 'jquery'],
            DataTable : 'datatables.net-bs'
        })

Any idea or hint would be highly appreciated.

Best, Chris

0 likes
4 replies
Yamen's avatar

You need to require it as follows:

window.datatables = require('datatables.net-bs');
legreco's avatar

What version of laravel are you using?

Chris1904's avatar

I was using 5.5.17 and the issue appeared to be with jquery.slim. Once I imported jquery it started working.

sybozz's avatar

This is due to a conflict of jquery. If you are loading the jquery in app.blade.php, the app.js makes the conflicts. As I have experienced the same issue and comment out the app.js. that makes my datatable working as expected.

Please or to participate in this conversation.