MomentJS doesn't load locales

2019-07-23 22:45发布

问题:

I am building something with moment and nodejs, but for some reason, the locales don't get loaded. Here is some code:

import moment from 'moment'

moment.locale('fr');
console.log(moment.locales());

So the console.log only returns the en locale so I suppose that the other locales aren't loaded.

However, the documentation says:

If there is a locale file in moment-root/locale/ named after that key, the first call to moment.locale will load it.

When I check in my node_modules/moment/locale, I have indeed a fr.js file.

So what am I doing wrong? Am I missing something to load my locales?

Thanks a lot for your help!

EDIT: I am using version ^2.14.1 of momentjs

回答1:

According to documentation please make sure that you have imported locale files or used minified file:

locale/*.js

or

min/moment-with-locales.js


回答2:

I found the solution, I needed to import the locale into my main js file:

import 'moment/locale/fr';