Moment installed via npm, .locale() dysfunctional

I’ve been experimenting with moment, installed via npm and I’m having trouble getting some parts of it to work. And I’m not sure if I understand the concepts correctly.

I’ve used moment successfully with meteor and with the corresponding locale-packages from atmosphere. I’m using german locales in my app and use moment.locale('de') to have german time formats in german (like ‘28. Dezember’ instead of ‘December, 28th’). This works with the the package rzymek:moment-locale-de. As far as I can tell it does not much: include the german locale.js from moment, and then set the scope of the moment variable:

// client.js
global = this;
global.moment = moment;

// server.js
global.moment = moment;

Now, I’m not really sure why, but this takes care that moment.locale('de') works.

When removing all moment packages that are installed via meteor add, switching to meteor release 1.3-beta.11 and installing moment via npm (aka omitting the wrapper package for meteor) then `moment.locale(‘de’) does not work anymore.

For example:

import moment from 'moment'

Person = class Person {
    /**
     * @returns {String}
     */
    getBirthDateFormatted () {
        moment.locale('de')
        return moment(this.get('birthDate')).format('LL')
    }
}

This code still returns the birthDate of that Person in us format (September 20, 1981). moment.locale('de') also always returns en.

I’m sure this is a problem of the scope in which moment tries to set it’s locale-settings, but I’m not sure how to fix it. I tried the solution from the rzymek:moment-locale-de package, by setting saving moment in the global object, but thats not working.

When connection to the server via meteor shell it kind of works, but only when using let moment = require('moment'), like here:

// connected via meteor shell
let moment = require('moment')
moment.locale('de')
// 'de'
let myDate = 'Tue Dec 10 1980 00:00:00 GMT+0100 (CET)'
moment(MyDate).format('LL')
// '10. Dezember 1980'

This only works when using require() instead of the import xy from 'xy' method. It’s mysterious to me why.

So the question is: How do I get moment.locale() to work as before switching to npm? I’m not sure that I’m approaching this the right way and would appreciate some hints into the right direction.

3 Likes

@sashko @benstr I heard you guys talking about the scope situation with npm in transmission#4 - Is this maybe kind of related? Could one of you maybe explain why scope with NPM could be an issue (and why that may be related to my problem)?

Thanks :slightly_smiling:

Are you also using the Moment locale package from NPM? can you show where and how you are importing them?

As far as I know there’s no moment-locale package on npm - the moment package already contains all existing locales (at least de). I added it to the package.json of my app ("moment": "*"), and imported it everywhere I use with import moment from 'moment' (second code snippet above).

Having the same problem as soon as I upgrade to Meteor 1.3. Found this “known bug”: http://momentjs.com/docs/#/use-it/browserify/

Now I have to figure out how specify for NPM, as I was just installing moment as a meteor package…

1 Like

I found a workaround for this:
If you want to use the fr locale, you have to write

import ‘moment/locale/fr’;

I don’t event know how it’s working, but it works.

3 Likes

In the moment npm package, you have:

node_modules/moment/locale/XX-xx.js

IIF which bootstraps moment and apply the localization

2 Likes

Works for me, thanks !

1 Like

For me, on Metero 1.3, I did this:

import '/node_modules/moment-timezone/node_modules/moment/locale/en-gb.js';

Thanks! it works perfectly!

Meteor 1.5 , moment as npm module
import ‘/node_modules/moment/locale/cs.js’;