Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

daigo's avatar
Level 1

using Flatpickr w/ Webpack

Hi everyone,

On a Laravel 5.4 project, I want to use the Flatpickr with Wepback, thus I pulled it in via yarn add -dev flatpickr and put the following lines in my bootstrap.js:

const Flatpickr = require('flatpickr');

My app.js file looks like this:

require('./bootstrap');

$(document).ready(function() {
    $('.js-datepicker').flatpickr({
        altInputClass: '',
        altInput: true,
        altFormat: 'd.m.Y',
        dateFormat: 'Y-m-d',
    });
});

Unfortunately, I get this error: Uncaught TypeError: $(...).flatpickr is not a function.

So I tried to use another approach:

$(document).ready(function() {
    const Flatpickr = require('flatpickr');
    new Flatpickr(document.querySelectorAll('.js-datepicker'), {
        altInputClass: '',
        altInput: true,
        altFormat: 'd.m.Y',
        dateFormat: 'Y-m-d',
    });
});

But this doesn't work either: Uncaught ReferenceError: Flatpickr is not defined and Uncaught TypeError: Cannot read property 'add' of undefined.

In another Laravel 5.3 project, where I use gulp instead of webpack, it works.

Can you help me to spot the error?

Thanks in advance and have a nice day :)

0 likes
2 replies
retracenz's avatar
const flatpickr = window.flatpickr = require("flatpickr");

$(document).ready(function() {
    new flatpickr(document.querySelectorAll('.js-datepicker'), {
        altInputClass: '',
        altInput: true,
        altFormat: 'd.m.Y',
        dateFormat: 'Y-m-d',
    });
});

This works for me

ejdelmonico's avatar

The package is probably not exported as a module so you can either do an ES6 import or do as @retracenz suggested and define it in /resources/assets/js/app.js by attaching it to the window (global).

Please or to participate in this conversation.