Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RequireJs with moment.js does not trigger `init`

My requirejs config is declaring moment.js and I'd like to set the default locale when requiring the library. So, I've tried this :

require.config({
  enforceDefine: true,
  noGlobal: true,
  baseUrl: '/js',
  shim: {
    'moment': {
      init: function (moment) {
        console.log("*** Moment loaded!");
        return moment;
      }
    }
  },
  paths: {
    'moment': 'lib/moment-with-locales.min'

  }
});

However, calling require(['moment'], function (moment) { ... }); does not invoke the init function inside the shim configuration.

What am I not getting, here?

like image 826
Yanick Rochon Avatar asked Jun 11 '26 06:06

Yanick Rochon


1 Answers

Moment.js defines itself as an AMD module and RequireJS won't call init.

From the documentation of RequireJS:

The init function will not be called for AMD modules. For example, you cannot use a shim init function to call jQuery's noConflict. See Mapping Modules to use noConflict for an alternate approach to jQuery.

From the master source of Moment.js:

https://github.com/moment/moment/blob/develop/moment.js#L2796

like image 166
Daniel A. White Avatar answered Jun 13 '26 15:06

Daniel A. White



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!