Search code examples
node.jsmomentjsmoment-timezonemoment-range

Moment.js: "TypeError: momentRange.range" when using with moment-range


I have problem using all 3 of the packages together. I define them like this:

var moment = require('moment-timezone');
var momentRange = require('moment-range');

And when I want to use the moment-range functions, I'm trying to call it like this:

var range1 = momentRange.range(moment("string1"), moment("string2"));

And I'm getting error: TypeError: momentRange.range is not a function

What am I doing wrong?


Solution

  • According to the documentation, you are supposed to use the moment-range library to first extend the core moment library itself, then use moment.range because the moment-range package adds additional functions to the moment object:

    var momentRange = require('moment-range');
    momentRange.extendMoment(moment);
    
    moment.range(moment(…), moment(…)); // Now usable
    

    Specifically, in their documentation:

    CommonJS:

    const Moment = require('moment');
    const MomentRange = require('moment-range');
    
    const moment = MomentRange.extendMoment(Moment);