3

I'm new to RequireJS, and I'm trying to load a plugin using the shim technique. Ideally I'd like to keep the plugin in a different directory as well.

No matter what I do, I get a script error (even when the plugins are in the base directory.) Here's my RequireJS config. picker.date.min requires picker.min

require.config({
    baseUrl: '/js',
    paths: {
        jquery:        'vendor/jquery/jquery-2.0.3.min',
        pickadate:     'vendor/pickadate/picker.min',
        pickadatedate: 'vendor/pickadate/picker.date.min'
    },
    shim: {
        jquery: {
            exports: '$'
        },
        pickadate:     ['jquery'],
        pickadatedate: ['jquery', 'pickadate']
    }
});

Here's the script I use on my page

require(['jquery', 'pickadate', 'pickadatedate'], function($) {

    $('#start_date').pickadate();

});

The error I get is: GET http://domain.com/js/pickadate.js 500 (Internal Server Error) require-jquery.js:1854 Uncaught Error: Script error

Can anyone help?

Leon
  • 715
  • 1
  • 8
  • 22
  • 1
    If ``pickadatedate`` requires ``pickadate`` your shim should be ``pickadatedate: ['jquery', 'pickadatedate]`` – Nick Tomlin Nov 14 '13 at 21:16
  • also you need to pass what required into your module – Igor Benikov Nov 14 '13 at 21:16
  • @NickTomlin I assume you meant the shim should be `pickadatedate: ['jquery', 'pickadate]`? I updated the code above, to what I have now, and it's still causing the same errors. The shims seem to compleely ignore the paths. Am I doing it right? – Leon Nov 15 '13 at 08:34
  • @IgorBenikov I'm not sure I understand what you mean? – Leon Nov 15 '13 at 08:35
  • I also just tried wrapping the plugins in `define(['jquery']...` as mentioned in [This answer](http://stackoverflow.com/questions/10918063/how-to-make-a-jquery-plugin-loadable-with-requirejs?codekitCB=406198040.963983) to no avail. It just appears to be ignoring my paths completely – Leon Nov 15 '13 at 08:55
  • Did you eventually get this to work? How? – stackunderflow May 03 '14 at 19:14

2 Answers2

1

jquery is already AMD compatible so you don't need shim for it. Also pickadatedate doesn't need to dependency on jquery because it is already implied by being dependent on pickadate. You shim can be simplified as:

shim: {
    pickadate:     ['jquery'],
    pickadatedate: ['pickadate']
}

However this change is not going to solve your problem. The error you mentioned is actually not typically caused by missing scripts. If requirejs wasn't able to locate the script then you would also see 404 GET requests in console (use Ctrl + Shift + I in Chrome). If you are not seeing 404 GET then missing script is likely not issue.

The error you mentioned typically means one or more scripts has some kind of syntax error that fails to load by JavaScript interpreter. That is, requirejs did found the script but when it tried to load it, it failed because of compilation or runtime issues.

The best way to find out the culprit script is by starting dev tools such as those available in Chrome and see the point of exception and values set in exception and any variables around it. If you don't see it then turn on the Break on Errors (see JavaScript: Is there a way to get Chrome to break on all errors?). This will cause Chrome debugger to break as soon as the error occurs. If the error is not script error then continue until you stop at above error.

Generally when I get above error, I just look at the last code I'd typed and I usually find some syntax error (BTW, I highly recommend "use strict"; and jslint to detect this even faster.

Community
  • 1
  • 1
Shital Shah
  • 63,284
  • 17
  • 238
  • 185
  • Thanks for this. I'll simplify my shim, and try it. I understand what you mean about 404 errors. The thing is, RequireJS is looking for those files in `js/pickadate.js`, (where the script is actually in `js/vendor/pickadate/picker.min.js`) – Leon Nov 15 '13 at 10:27
1

You must have solved this by now but for others who come here, the problem IS your paths. Try using relative paths instead, I had the same issue and that fixed it for me. Something like this:

paths: {
    jquery:        '../vendor/jquery/jquery-2.0.3.min',
    pickadate:     '../vendor/pickadate/picker.min',
    pickadatedate: '../vendor/pickadate/picker.date.min'
},
Obi
  • 3,091
  • 4
  • 34
  • 56