Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

34Uncaught (in promise) TypeError: moment is not a function(…) #116

Closed
ItsMrAkhil opened this issue Dec 18, 2016 · 15 comments
Closed

34Uncaught (in promise) TypeError: moment is not a function(…) #116

ItsMrAkhil opened this issue Dec 18, 2016 · 15 comments

Comments

@ItsMrAkhil
Copy link

ItsMrAkhil commented Dec 18, 2016

.
.
.
.
.
.
.
}(this, function(moment, $) {
    var DateRangePicker = function(element, options, cb) {

        //default settings for options
        this.parentEl = 'body';
        this.element = $(element);
        this.startDate = moment().startOf('day');
.
.
.
.


Error Image

@skratchdot
Copy link
Owner

Can you past a link to your code? It seems like you aren't passing moment and $ to the function?

@minusplusminus
Copy link

Same problem here. moment() isn't a function moment.default() is

@alikhani
Copy link

alikhani commented May 2, 2017

I'm having the same issue. Any timeline for when this could get fixed?

@stuneak
Copy link

stuneak commented Jul 19, 2017

@skratchdot I'm having the same issue, how to fix it?

@paknefia
Copy link

paknefia commented Nov 4, 2017

I'm having the same issue. And solved by calling moment.min.js file before daterangepicker.js in the end of html body.

@ghost
Copy link

ghost commented Jan 8, 2018

Same issue here. I made sure that I include moment.js dependency before react-bootstrap-daterangepicker one. I am using webpack for building the app, and getting the same error as posted. All of my other moment.js applications are working as expected.

@WebKieth
Copy link

Same. Try to init daterange into my Vue component. Problem starts from migration (browserify to webpack) on browserify + vueify moment works fine

@ciolansteen
Copy link

Not sure this isn't yet fixed but - Load moment.min.js before daterangepicker.js

@chrislabarge
Copy link

Not sure this isn't yet fixed but - Load moment.min.js before daterangepicker.js

Thanks @ciolansteen ^^ this solved it for me!

@krishna-kb
Copy link

krishna-kb commented Oct 29, 2018

I faced the same issue and I use webpack in my project. Little bit of digging into bootstrap-datepicker code helped me solve this.
I made some changes to my webpack config to bypass this issue,

Add this to 'modules' in webpack config, where all the rules are
This bypasses the define check which is in daterangepicker.js file.

rules: [
      {
        test: require.resolve('path_to_node_modules/bootstrap-daterangepicker/daterangepicker.js'),
        use: 'imports-loader?define=>false',
      },
]

Now we need to expose our moment.js module on window to bypass the ternary condition and avoid getting the moment module from require.js

new webpack.ProvidePlugin({
      'window.moment': ['moment', 'default'],
    }),

PS:
I decided to solve this purely using webpack as there are other components in my repo which use moment.js, so I didn't want to include moment in my index.html as I want it to be lazy loaded.
This should solve the issue a little bit cleanly, which avoids unnecessary importing moment via script tag.

@erisriso
Copy link

Not sure this isn't yet fixed but - Load moment.min.js before daterangepicker.js

that solved my problem

@skratchdot
Copy link
Owner

closing this due to inactivity. please re-open if this is still an issue in v6.0.0 or greater

@monodeep88
Copy link

I'm having the same issue. And solved by calling moment.min.js file before daterangepicker.js in the end of html body.

yes as there are moment() func in daterangepicker.js file..so we need to call the moment.min.js file before daterangepicker.js.

@manojKonda01
Copy link

closing this due to inactivity. please re-open if this is still an issue in v6.0.0 or greater

@skratchdot am facing this same issue.
Iam using
"bootstrap-daterangepicker": "^3.1.0",
"jquery": "^3.6.4",
"moment": "^2.29.4",
"numeral": "^2.0.6",
"react": "^18.2.0",
"react-bootstrap-daterangepicker": "^8.0.0",

@hassamulhaq
Copy link

hassamulhaq commented Dec 7, 2024

Laravel: v11,
Vite: v5.0

How to fix 'Uncaught (in promise) TypeError: moment is not a function with datarangepicker plugin'

npm install jquery
npm install daterangepicker
npm install moment

add below code in app.js

app.js path --> /resources/js/app.js

import $ from "jquery";
window.jQuery = window.$ = $;

import * as moment from 'moment';

import 'daterangepicker';

in resources/css/app.css

@import 'daterangepicker/daterangepicker.css';

vite config file

vite.config.js --> vite.config.js check resolve: {...

import {defineConfig} from 'vite';
import laravel from 'laravel-vite-plugin';

import path from 'path';

export default defineConfig({
    server: {
        watch: {
            usePolling: true,
            ignored: [
                '**/node_modules/**',
                '**/vendor/**',
            ],
        },
    },
    plugins: [
        laravel({
            input: [
                'resources/css/app.css',
                'resources/js/app.js',
            ],
            refresh: true,
        }),
    ],
    resolve: {
        alias: {
            '~bootstrap': path.resolve(__dirname, 'node_modules/bootstrap'),
            '$': 'jQuery',
            'moment': path.resolve(__dirname, 'node_modules/moment/moment'),
        }
    }
});

How to use.

type="module" is important
index.blade.php

<script type="module">
const initializeDateRangePicker = () => {
$('#monthly-date-range').daterangepicker({
    opens: 'center',
    locale: {
        format: 'YYYY-MM-DD'
    },
    startDate: ---,
    endDate: ---
});
initializeDateRangePicker();
</script>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests