简体   繁体   中英

Trouble with a package (moment-recur) when moving from Bower to NPM

Background: I'm trying to move this package Moment-recur from Bower to NPM in our Angular 1.5 app and having some trouble. The package depends on Moment and extends its function as

moment.recur = function(start, end) {
    // If we have an object, use it as a set of options
    if (start === Object(start) && !moment.isMoment(start)) {
        return new Recur(start);
    }

    // else, use the values passed
    return new Recur({ start: start, end: end });
};

src

This is what I did: Uninstalled the package from bower, installed through npm and updated index.js where the package is referenced:

From:

window.moment = require('moment');
require('Bower_Components/moment-recur/moment-recur.js');

To:

window.moment = require('moment');
require('moment-recur');

Problem: When I run our unit tests (karma), all the tests referencing recur function are breaking. The error I see is:

TypeError: moment(...).recur is not a function

I included console log on the Moment-Recur package's js file and could see that when I run the app. However, I don't understand why recur function is not available on moment.

I'm lost here and don't know how to figure out the cause. Let me know if you need me to post any other configuration settings from the app.

I eventually ended up fixing the issue. Though it's been more than a year since I asked the question, I thought the answer (from what I remember) may help someone.

What fixed the issue in my case was doing this:

window.moment = require('moment-recur')

instead of

window.moment = require('moment')

I believe it's because of the way recur handles the moment package. Cursory look at the src code for recur seems to indicate it returns a moment object with some of it's functionality attached. That might explain why attaching just moment-recur would suffice in my case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM