'Use npm datetimepicker library in Laravel 8

I am trying to use a library installed through npm: https://github.com/xdan/datetimepicker

I add it to the file in resources app.js next to jquery:

require('bootstrap');
window.$ = require('jquery');
window.jQuery = require('jquery');
window.datetimepicker = require("jquery-datetimepicker");

I have also tried to add it like this:

window.$.datetimepicker = require("jquery-datetimepicker");

And in page I use this code:

 $(document).ready(function(){
    $('.date').datetimepicker({step:30});
})

But always get the same error: $(...).datetimepicker is not a function

If I add the libraries directly, it works.

<link href="/js/jquery.datetimepicker.min.css" rel="stylesheet">
<script src="/js/jquery.datetimepicker.full.min.js"></script>


Solution 1:[1]

Try the following...

window._ = require('lodash');
window.$ = window.jQuery = require('jquery');
require("jquery-datetimepicker");
require('bootstrap');

$(document).ready(function(){
    $('.date').datetimepicker({step:30});
})

And make sure to add {{ mix('css/app.css') }} at the top and {{ mix('js/app.js') }} at the bottom.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Karl Hill