Moment: './locale' path not found in moment/src/lib/locale/locales.js

Created on 19 Mar 2018  ·  54Comments  ·  Source: moment/moment

WARNING in ./node_modules/moment/src/lib/locale/locales.js
Module not found: Error: Can't resolve './locale' in 'D:\project-fed\ecloud\client-web\node_modules\moment\src\lib\locale'

I found that there is something wrong with the function below

function loadLocale(name) {
    var oldLocale = null;
    // TODO: Find a better way to register and load all the locales in Node
    if (!locales[name] && (typeof module !== 'undefined') &&
            module && module.exports) {
        try {
            oldLocale = globalLocale._abbr;
            var aliasedRequire = require;
            aliasedRequire('./locale/' + name);
            getSetGlobalLocale(oldLocale);
        } catch (e) {}
    }
    return locales[name];
}

aliasedRequire('./locale/' + name);, should it be ../locale/ ?

Troubleshooting

Most helpful comment

Just import like below and it's work :

import moment from 'moment/moment';

All 54 comments

I see this same warning when I use webpack to bundle up moment. Any idea when this error was introduced?

I have the same warning using angular-cli any solution?

Having the same issue. Do you know of any workarounds / fixes?

Same issue - looks like a path issue with './locale' vs '../locale'. Is anyone looking into this bug?

https://github.com/moment/moment/issues/2979

Looks like it's been the case for years, why they won't fix it I have no idea. I just ran into this as well when trying to use jsnext:main in webpack instead of main.

My work-around to keep using jsnext:main in webpack for the other packages that have properly transpiled es6 code: use an alias to force webpack to use moment.js directly as it is specified in pkg.main

... resolve: { alias: { moment$: path.resolve(root_dir, "node_modules/moment/moment.js") } }

The above didn't work for me but following this tutorial helped fix it for me.

https://alligator.io/angular/custom-webpack-config/

Is there anything new about this? I'm using Laravel Mix... same thing here...

I do

import * as moment from 'moment';

and that works.

I do

import * as moment from 'moment';

and that works.

inside the webpack? I am thinking about how to do this with Laravel Mix..

@marcelogarbin oh sorry I haven't messed with Laravel since version 4. I apologize but .I don't know how to make it work with laravel mix.

Just import like below and it's work :

import moment from 'moment/moment';

@yaasinhamidi's solution did the trick

I do

import * as moment from 'moment';

and that works.

Where should this line be put?

Experiencing this same issue for the first time after updating to 2.25.0. I am also using Laravel Mix (note, i never experienced this issue on any previous versions).

my workaround is to get back to 2.24.0

I am facing the same issue after updating

Until it is fixed, rolling back to v2.24.0 solves this issue.

Rolling back to v.2.24.0 worked for me as well.

for anyone else experiencing this issue, i had to also force the versions of moments that other packages in my node_modules/ folder pulled in as dependencies to v2.24.0 as well

using yarn, adding this line to package.json sorted things for me

"resolutions": {
        "**/moment": "2.24.0"
    },

Hello Guys!
I faced the same issue from last few hours and finally I have resolved it through changing moment version.

In the above comments i saw the final solution.

Reason:
_moment release the new version 17 hours ago from 2.24.0 to 2.25.0_

Solution:
Step 1: change the versions

  • "moment": "2.24.0",
  • "moment-timezone": "^0.5.28",

Step 2: If you are using yarn please add resolutions in your package.json file like this
"dependencies" {
"moment": "2.24.0",
"moment-timezone": "^0.5.28"
},
"resolutions": {
"moment": "2.24.0"
},

Thank You.

Even if you have, following block. The package-lock.json will create a new version i.e. 2.5.1. So change it to "moment": "2.24.0" till the issue is addressed.
"dependencies" {
"moment": "^2.24.0", ----------------> "moment": "2.24.0"
},

i rolled back to 2.24.0 but still having same warning while using node

It's weird I started getting this error today after re installing dependencies, the suggested fix of ignoring the locale plugins with webpack didn't work. What worked was leaving the version fixed at 2.24.0 as suggested by @sachins81 . I wonder if something relevant changed in recent commits? @ichernev

i rolled back to 2.24.0 but still having same warning while using node
@nitindevelopermca Make sure it's "2.24.0" and not "^2.24.0". (Worked for me after changing it)

I just rolled back to "^2.18.1" and everything seems working fine

Same issue using react, moment isnt a dependancy of my project so I can't roll it back

Same issue using react, moment isnt a dependancy of my project so I can't roll it back

You can run this commands for now.

npm install [email protected] --save

I had the same case and when I search somehow antd was using moment as a dependency and i hope the moment issue will be closed in next release while i saw they released a new version so please first try the latest version which is 2.25.1 then try to add moment as a dependency.

Thank You!

It helped me to change to the fixed version 2.24.0 inside the dependencies and inside the resolutions. I use the package inside my code and as a dependency of react and antd.

If you have several workspaces don't forget to change in all package.json files. I forgot this at first.

same problem happened in v2.25.1, then I try to add these in package.json(I use yarn):

"dependencies" {
  "moment": "2.24.0"
},
"resolutions": {
  "moment": "2.24.0"
},

Same error here. I heve

"dependencies": {
    "moment": "^2.25.1"
}

and when I run npm run production I get:
shell script WARNING in ./node_modules/moment/src/lib/locale/locales.js Module not found: Error: Can't resolve './locale' in '/var/www/apf.local/html/node_modules/moment/src/lib/locale' @ ./node_modules/moment/src/lib/locale/locales.js @ ./node_modules/moment/src/lib/locale/locale.js @ ./node_modules/moment/src/moment.js @ ./resources/js/app.js @ multi ./resources/js/app.js

With ~2.24.0 version, everything working as expected.

Hello Guys!
I faced the same issue from last few hours and finally I have resolved it through changing moment version.

In the above comments i saw the final solution.

Reason:
_moment release the new version 17 hours ago from 2.24.0 to 2.25.0_

Solution:
Step 1: change the versions

  • "moment": "2.24.0",
  • "moment-timezone": "^0.5.28",

Step 2: If you are using yarn please add resolutions in your package.json file like this
"dependencies" {
"moment": "2.24.0",
"moment-timezone": "^0.5.28"
},
"resolutions": {
"moment": "2.24.0"
},

Thank You.

Just import like below and it's work :

import moment from 'moment/moment';

great! it works!

Thanks .
Its work fine. just import the line bellow:
import * as moment from 'moment/moment';

Just import like below and it's work :

import moment from 'moment/moment';

Fantastic fix :) solved a major headache with this

Fantastic fix :) solved a major headache with this

Mediocre fix, if someone doesn't have antd as dependency directly.

I've encountered the same problem but I don't have momentjs in dependencies. But antd does.
And npm can't work with resolutions.

The only solution for me right now is downgrading antd.
I can't see proper solution which wouldn't be including extra package to make things work.

I have the same issue in my React app which, for some reason, started to show up out of the blue. I've been using moment in this app forever and had never noticed this error. It actually became an error in my case as my React app wouldn't compile because of this issue.

I then tried import moment from 'moment/moment'; which fixed the compilation issue but I still get the following warning everytime I run the app.

./node_modules/moment/src/lib/locale/locales.js
Module not found: Can't resolve './locale' in 'C:\Users\myname\source\repos\myapp\node_modules\moment\src\lib\locale'

The only saving grace is that my React app, at least, compiles and runs fine.

I have the same issue with create-react-app (typescript template) too.I tried import moment from 'moment';.Then I got the error :
./node_modules/moment/src/lib/locale/locales.js Module not found: Can't resolve './locale' in 'D:\MyName\Learning\ProjectName\node_modules\moment\src\lib\locale'

@devayarhlaine I have post a temporary solution in above comments please check it.

Its was a moment new version issue.

Same s above:
I have the same issue with "Cannot find module 'moment'" too.
Using the solution with "import * as moment from 'moment/moment' solved one issue.
But then I got the error :
./node_modules/moment/src/lib/locale/locales.js Module not found: Can't resolve './locale' in 'D:\MyName\Learning\ProjectName\node_modulesmoment\src\lib\locale'
and downgrading isn't a solution (in my opinion :-).

Got this error when upgrading from angular 8 to angular 9.1.
Upgrading to moment 2.25.1 solved the issue #5486 ("TS2307: Cannot find module 'moment'. "), but the locale error remained.

For those using the command "yarn create react-app ", the fix is to edit node_modules/react-scripts/config/webpack.config.js to add the alias like below.

moment$: 'moment/moment.js'

Insert the above line into the alias block at line number 311.

alias: {
  // Support React Native Web
  // https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future-with-react-native-for-web/
  'react-native': 'react-native-web',
  // Allows for better profiling with ReactDevTools
  ...(isEnvProductionProfile && {          'react-dom$': 'react-dom/profiling',
  'scheduler/tracing': 'scheduler/tracing-profiling',        }),
  ...(modules.webpackAliases || {}),        
  moment$: 'moment/moment.js'
},

Another way to solve this is to replace moment.js with Day.js. Follow the instructions in this url to use react-app-rewired >> https://ant.design/docs/react/use-with-create-react-app. Modify package.json according to the instruction. Then create a config-overrides.js in the root folder like below. This has the benefit of shrinking the js from 231.11kb to 11.11kb according to this https://github.com/ant-design/antd-dayjs-webpack-plugin.

const { override, addWebpackPlugin } = require('customize-cra');    
const AntdDayjsWebpackPlugin = require('antd-dayjs-webpack-plugin');

module.exports = override(
        addWebpackPlugin(new AntdDayjsWebpackPlugin())
);

Just import like below and it's work :

import moment from 'moment/moment';

Fantastic fix!!!

Just import like below and it's work :

import moment from 'moment/moment';

Worked for me

loooks like root of issue was caused by "module" field in package.json (https://github.com/moment/moment/commit/9ce89e7fea881b39be23b8c0646f7ef7817985d9)

here is PR to fix it https://github.com/moment/moment/pull/5503

Yup, removing "module": "./src/moment.js", from package.json made the warning go away for me. BTW, mine is a React app -- create-react-app and I'm using "moment": "^2.25.1". Even though I was using the latest version, I was still getting this error but updating the package.json seems to fix the issue. Thanks @ApacheEx

I have fix this error, at least for me.

I went to node_modules/moment/src/lib/locale/
Modified the locales.js file, ctrl+f and search './locale'+name line.
Change it to './'+name.
I don't know why is pointing to same folder and '/.locale' + name, this is why the error says can't resolve ./locale

To be honest, I copied the original line and commented just above, if in the future an error shows up.

If it helps, thumbs up. This kind of errors are the worse...

Having the same problem, reverted to 2.24 until a fix is available. I can't edit the module files, as i'm building on an automatic server, so waiting for an official release.

I have the same issue with 2.24 and 2.25.1 version
WARNING in ./node_modules/moment/src/lib/locale/locales.js

When I open browser console, I see this error:
TypeError: Cannot read property 'defineLocale' of undefined at locales.min.js:1

We are having the same issue with moment 2.22

Just import like below and it's work :

import moment from 'moment/moment';

Where should this line be put?

Just import like below and it's work :
import moment from 'moment/moment';

Where should this line be put?

In the file that you want use momentjs on ecmascript like below:

import moment from 'moment/moment';
(() => {
const now = moment(moment.now());
console.log(now);
})

2.25.3 should fix this issue.
Please open another issue if you still have challenges.
Or, you can try Stack Overflow for more individual questions.

This alias with the WebPack config fixes an issue for me:

resolve: {
  extensions: ['.js', '.ts'],
  alias: {
    './locale': 'moment/locale'
  }
}

If you are using moment-timezone:
https://github.com/moment/moment-timezone/issues/837

i fix this with this:

import moment from 'moment'; import 'moment/locale/pt-br';

it works, and the locale is set to 'pt-br', and all others languages works too.

Was this page helpful?
0 / 5 - 0 ratings