General developer forum

AMD Module moment.js loaded via require.js produces mistmatch error

 
Picture of David Lowe
Re: AMD Module moment.js loaded via require.js produces mistmatch error
 

That's funny, I spent a good 2 hours trying to figure this out, posted here then figured it out 5 minutes later.

In my case I'm trying to require moment.js in a local plugin. My files are in local/[plugin_name]/amd/src/

Place the name of your plugin where you see [plugin_name] in my samples. 

To start my local plugin in index.php it includes 1 javascript file:

$PAGE->requires->js_call_amd('local_[plugin_name]/some_javascript_file', 'init');

In my some_javascript_file.js I use bootstrap-datetimepicker which needs moment.js so my define looks like this:


define(['jquery', 'local_[plugin_name]'/bootstrap_datetimepicker'], function ($, datetimepicker) {

   return {

      ....other functions here

      init: function () {

         ....whatever code here....

      }

});


Now when require goes to get datetimepicker it is going to look in local/[plugin_name]/amd/src/bootstrap-datetimepicker.js

The bootstrap-datetimepicker will then require moment.js but won't find it.

I believe this is because it's the way Moodle (or require?....not sure) handles locations. So in my bootstrap-datetimepicker.js file 

where it calls the define I had to add the path to moment.

define(['jquery', 'local_[plugin_name]/moment'], factory);

instead of it's default of:

define(['jquery', 'moment'], factory);

and voila, it works. It's not ideal because now that means I have to change files that I never want to touch..........oh well, at least it's working for now.

HTH

 
Average of ratings: -
Picture of Henrik Thorn
Re: AMD Module moment.js loaded via require.js produces mistmatch error
 

You can avoid changing files by adding paths to your requirejs, which is added just above your "some_javascript_file" define statement. 


requirejs.config({
  paths: {
    "moment": "[theme_something]/moment"
  }
});

 
Average of ratings: -