jkbrzt / rrule

JavaScript library for working with recurrence rules for calendar dates as defined in the iCalendar RFC and more.
https://jkbrzt.github.io/rrule
Other
3.34k stars 513 forks source link

Using with Require.js is failing? Please help #81

Closed chriswible closed 6 years ago

chriswible commented 9 years ago

I'm having a very hard time implementing RRule with Require.js.

The rrule Changelog says there's Require.js support so I would expect to be able to do something like:

define([ 'jquery', 'underscore', 'backbone', '../../rrule/rrule', '../../rrule/nlp', ], function ($, _, Backbone, RRule, NLP) { ...

Or something similar. However, all variations I've tried result in this error:

Uncaught Error: Module name "rrule" has not been loaded yet for context: _

I've searched all over and I can't find an example of RRule being used with require in the wild. Is there any chance you could help me out? I think other users would benefit as well.

Thank you for your time, I'm so psyched to use your work.

Chris

chriswible commented 9 years ago

This is not for server-side use, by the way. I've also read the RequireJS docs for this error and followed their recommendations. (http://requirejs.org/docs/errors.html)

chriswible commented 9 years ago

Interesting that the error goes away when I put rrule and nlp in the same file. It would appear that the error is being caused by nlp?

RRule works for me, but I have to merge the two files (rrule.js and nlp.js) in order for that error to go away. Am I missing something?

jkbrzt commented 9 years ago

I don't think loading of the .js files via RequireJS has ever been tested. require() works properly when the scripts are already loaded though:

http://jsfiddle.net/bjfyojg5/3/

(https://github.com/jakubroztocil/rrule/blob/58af512881ddfc2da8e6eb90fab213497b2d6cb4/lib/rrule.js#L1903-1908)

chriswible commented 9 years ago

Thanks for looking into it for me, I've been quite confused. In your changelog, under 1.1.0, you said you added support for RequireJS - I took that to mean I could include rrule and nlp as requirejs modules and load them via js.

Thanks for checking though.

jkbrzt commented 9 years ago

RequireJS has been added via a pull request someone has opened. Feel free to look into fixing this.

davidsidlinger commented 9 years ago

@chriswible Because nlp.js has an implicit dependency on rrule.js, you need to shim it in your configuration. (At least, this is what worked for me.)

requirejs.config({
    paths: {
        "rrule": "path/to/rrule/rrule",
        "rrule-nlp": "path/to/rrule/nlp"
    },
    shim: {"rrule-nlp": {
            deps: ["rrule"]
        }
    }
});
jpuffer commented 7 years ago

For me, I got the message: Module name "nlp" has not been loaded yet for context: _ even though I had the shim as described above and I was including rrule-nlp as a dependency on the module where I was using it. My fix was to rename the module in my require configuration from rrule-nlp to nlp (to match the Require error I was getting.)

It seems like Require is looking for the nlp module by name rather than by path, although this line seems to be referencing it by path: https://github.com/jakubroztocil/rrule/blob/master/lib/rrule.js#L2271

davidgoli commented 6 years ago

Please reopen if you are still experiencing issues.