Moment: Webpack Error: Cannot find module "./locale" after updating to 2.19.0

Created on 10 Oct 2017  ·  149Comments  ·  Source: moment/moment

Edit: If you set your dependency to 2.18.1, everything will be fine. We are coordinating with some other libraries to find a permanent solution
Edit 2 : Set "moment": "2.18.1" in your package.json
Edit 3: We believe this is fixed.

Hello.
Using create-react-app. After updating moment to 2.19.0 i have this error on load :

Error: Cannot find module "./locale"
-> 9 stack frames were collapsed.
./src/utils/Utils.js
src/utils/Utils.js:1

1 | import moment from 'moment'

Most helpful comment

@patricksteenks : yes. Fix like this in you package.json "moment": "2.18.1"

All 149 comments

I have the same, confirming this error.

same here

Can confirm:

This relative module was not found:

* ./locale in ./node_modules/moment/src/lib/locale/locales.js

OK, we'll look into this, but it won't be very quick.

We changed the name of the require function, if someone can see/investigate if its linked to:

https://github.com/moment/moment/pull/4213
https://github.com/moment/moment/pull/4214

Same problem here. Would downgrading fix the problem?

Same here, broke multiple apps that I work on.

@patricksteenks : yes. Fix like this in you package.json "moment": "2.18.1"

@ichernev hey, but I'm using locale without importing. Just using import moment from 'moment' and use like this moment(date).locale('uk').format('ddd'). As I understood you changed import to locale. How I can fix this in my case?

Same issue on Angular-CLI built project

Confirmed.

VueJS 2 Webpack builds impacted too. Temp fix is as GautierT suggested (downgrade momentjs)

package.json (issue with moment-timezone dep on moment 2.19)

    "moment": "2.18.1",
    "moment-timezone": "^0.5.13",

package.json (issue in general)

    "moment": "2.18.1"

I can confirm this is also happening!

i can confirm.
package.json

"moment": "2.18.1"

@renedx just this line enough

Same in an Angular 4 application.
Just downgraded to 2.18.1 (strict version number in package.json)

Got hit by this as well. I suspect it is webpack doing some magic to recognize the dynamic "require", and can no longer recognize it under the new name, and thus the locales never get bundled into the package.

(Incidentally, this whole situation sounds an awful lot like exactly what ES6 dynamic imports are for. We can dream :-)

I use in webpack from this page

new IgnorePlugin(/^\.\/locale$/, /moment$/)

Now remove it

And in require

const moment = require('moment');

Now

const moment = require('moment').default;

Strictly speaking, I think the Angular connection is simply because most Angular projects use Angular-CLI, which internally uses Webpack. But it may be related to some specific Webpack configuration in that stack.
It does seem to be a fairly general bundling problem, though.

Repent, repent, doomsday is here!

Same here.

import moment from "moment"
import "moment/locale/fr"
moment.locale("fr")
WARNING in ./node_modules/moment/src/lib/locale/locales.js
Module not found: Error: Can't resolve './locale' in '/Users/kud/Projects/_contexte/lois-webapp/node_modules/moment/src/lib/locale'
 @ ./node_modules/moment/src/lib/locale/locales.js 65:16-60
 @ ./node_modules/moment/src/lib/locale/locale.js
 @ ./node_modules/moment/src/moment.js
 @ ./src/index.js

Same here - I fixed it with a feature of yarn to overload the default settings of a package:

place this in your main package.json (adapt "moment-timezone" to the name of the package which does load moment if you do not directly load moment):

  "resolutions": {
    "moment-timezone/moment": "2.18.1"
  },

just like an 10km asteroid .... same here ...

What is the ETA for fix?

Okay, here's where we're at:

  • If you set your dependency to 2.18.1, everything will be fine.
  • This is an issue with https://github.com/moment/moment/pull/4213
  • I have engaged both the webpack and react native teams to talk about what fix will work for all parties
  • Because setting the dependency to 2.18.1 provides an easy mitigation, I do not at this time want to roll back. Instead, I would like to wait on feedback from Webpack and React Native to see if we can come to an agreement about what to do that will move this situation forward. For that reason, I don't have an ETA on a fix.
  • A MCVE can be found here: https://github.com/mj1856/moment-webpack-test

Confirmed error when running react app created with create-react-app CLI.

Solved this downgrading moment and monent-timezone version.

npm install [email protected] as a temporary fix so you don't have to deal with updating package.json multiple times. This will probably get fixed soon, then you can do npm i.

@aheissenberger this saved my life, I had the same problem with rc-calendar, can confirm It works for yarn 1.2.0.

Same problem webpack 3.6.0 + moment 2.19.0. Worked fine with 2.18.1

Others might have my same problem: Even though I didn't update to 2.19.0, in my package.json I had ^2.18.1. I changed it to 2.18.1 without the ^, and now it will not install anything higher than 2.18.1 until we know 2.19.x is safe

@ichernev seems very likely that the rename of 'require' is causing these widespread breakages. I suspect that when you require modules using a reference like that it means that webpack is unable to detect the import since it uses AST static analysis to determine dependencies.

Perhaps you could provide some details of the React Native issue it was fixing as there might be some better way around that original issue?

In our project we're using yarn 1.0.2, create-react-app, and these deps in package.json

"dependencies": {
    "@types/history": "^4.5.0",
    "@types/jest": "^20.0.6",
    "@types/material-ui": "^0.18.2",
    "@types/moment-timezone": "^0.5.0",
    "@types/node": "^8.0.20",
    "@types/react": "^16.0.10",
    "@types/react-big-calendar": "^0.14.0",
    "@types/react-dnd": "^2.0.33",
    "@types/react-dnd-html5-backend": "^2.1.8",
    "@types/react-dom": "^16.0.1",
    "@types/react-modal": "^1.6.6",
    "@types/react-redux": "^4.4.40",
    "@types/react-router-dom": "^4.0.4",
    "@types/react-router-redux": "^5.0.0",
    "@types/redux": "^3.6.0",
    "@types/redux-logger": "^3.0.0",
    "@types/redux-promise-middleware": "^0.0.6",
    "@types/validator": "6.3.0",
    "lodash": "^4.17.4",
    "material-ui": "0.19.2",
    "material-ui-datatables": "^0.18.2",
    "material-ui-fullscreen-dialog": "0.7.1",
    "moment": "2.18.1",
    "moment-timezone": "^0.5.13",
    "react": "^15.6.1",
    "react-barcode": "^1.2.0",
    "react-big-calendar": "^0.15.0",
    "react-bootstrap": "^0.31.0",
    "react-bootstrap-table": "^3.4.2",
    "react-bootstrap-time-picker": "^1.0.3",
    "react-dnd": "^2.4.0",
    "react-dnd-html5-backend": "^2.4.1",
    "react-dock": "^0.2.4",
    "react-dom": "^15.5.4",
    "react-modal": "^1.7.7",
    "react-pdf-js": "^2.0.5",
    "react-phone-number-input": "^0.12.10",
    "react-print": "^1.3.1",
    "react-redux": "5.0.6",
    "react-router-dom": "^4.1.1",
    "react-router-redux": "next",
    "react-tap-event-plugin": "^2.0.1",
    "react-transition-group": "^1.1.1",
    "redux": "3.7.2",
    "redux-logger": "^3.0.1",
    "redux-persist": "^4.8.0",
    "redux-promise-middleware": "4.2.1",
    "reselect": "^3.0.1",
    "validator": "8.2.0",
    "zxcvbn": "^4.4.2"
 }

Although we're using "moment": "2.18.1", we're still getting
screen shot 2017-10-10 at 3 53 43 pm

Is there a workaround of the issue for yarn?

@ggsjyoon: I had a similar situation (still getting the error) until I remembered you'll need to update one of the files that WebPack is watching to have it regenerate and do away with any cached version.

+1

@ggsjyoon there might be an additional requirement from one of your packages (e.g. moment-timezone) - you can use my tip https://github.com/moment/moment/issues/4216#issuecomment-335489518 to fix this with yarn

I ended up getting past this by changing:

import * as moment from 'moment';

to:

import * as moment from 'moment/moment';

+1

@morrisonbrett This works. Thanks

Confirming @GautierT's suggestion works for me.

@maggiepint Seeing what worked for @morrisonbrett, I’m thinking that #4213 may not be the problem. 2.19 introduced another new change, the module field in package.json - ie previously webpack was using the transpiled version.

Turning on ES6 for react native is what caused most of the issues blocking #4187, and #3904 (on the develop branch only for quite a while) made the same change for all webpack users.

@tqc - it's probably both. @thelarkinn already confirmed that webpack would not like the dynamic requires.

Same issue here, any updates?

@maggiepint Yeah, I realised just after I wrote that comment that the bug at the end of the thread wasn't quite the same one at the start. The ES6 side of things might be fixable with something like module.exports = require(‘moment’).default, but I’m not sure if that will still allow tree shaking, and node harmony doesn’t like mixing import with require.

solved via webpack aliasing:

  resolve: {
    alias: {
      'moment$': 'moment/moment',
    },
  },

Can somebody figure out all the bits from 2.19 that broke webpack, propose a minimal PR (based on 2.19.0), that reverts the "bad" pieces, and we'll consider merging it for 2.19.1.

Saying "me too" for the 1000 time is not brining much to the table.

@morrisonbrett's solution works import * as moment from 'moment/moment';

I can confirm this same issue with Angular-CLI v1.4.4. I was initially surprised when I saw the error involving momentjs since my project doesn't use it but after investigating I traced its injection back to WebPack.

Here's my warning message when I build:

WARNING in ./node_modules/moment/src/lib/locale/locales.js
Module not found: Error: Can't resolve './locale' in '/Users/scott/Development/web-admin/node_modules/moment/src/lib/locale'
resolve './locale' in '/Users/scott/Development/web-admin/node_modules/moment/src/lib/locale'
  using description file: /Users/scott/Development/web-admin/node_modules/moment/package.json (relative path: ./src/lib/locale)
    Field 'browser' doesn't contain a valid alias configuration
  after using description file: /Users/scott/Development/web-admin/node_modules/moment/package.json (relative path: ./src/lib/locale)
    using description file: /Users/scott/Development/web-admin/node_modules/moment/package.json (relative path: ./src/lib/locale/locale)
      as directory
        /Users/scott/Development/web-admin/node_modules/moment/src/lib/locale/locale doesn't exist
[/Users/scott/Development/web-admin/node_modules/moment/src/lib/locale/locale]
 @ ./node_modules/moment/src/lib/locale/locales.js 65:16-60
 @ ./node_modules/moment/src/lib/locale/locale.js
 @ ./node_modules/moment/src/moment.js
 @ ./src/app/admin-analytics/admin-analytics.component.ts
 @ ./src/$$_gendir/app/app.module.ngfactory.ts
 @ ./src/main.ts
 @ multi ./src/main.ts

I have also tried the import * as moment from 'moment/moment'; method but no luck for my case.

Hey @kud, @AleshaOleg – if you are still trying to import locales:
instead of:
import 'moment/locale/*
try:
import 'moment/src/locale/*'

Have been fighting with the same issue over here...
–––
@AleshaOleg : in your use case i think it could be:
import moment from 'moment'
import 'moment/src/locale/uk'
and then just call moment(date).format('ddd')

Thank you @dialnco !

I ended up dealing with this problem, when updating to 2.19.0, by completely disabling the code that attempts to load locales.

({
  test: /moment\/src\/lib\/locale\/locales.js$/,
  enforce: 'pre',
  use: {
    loader: 'imports-loader',
    options: {
      module: '>undefined', // this disables the if
      require: '>false' // this prevents webpack from trying to parse the requires
    }
  }
})

I then manually added import 'moment/src/locale/ja', import 'moment/src/locale/pt-br', et al as needed by my application, instead of loading all locales. If you are creative you can use dynamic import()s instead to only load the language your application needs.

It is very important that, if you do this, that you do not do import 'moment/locale/foo', but import 'moment/src/locale/foo'. Because of how the locale files try to refer to moment itself, they will instead bundle a second copy of moment (this time the UMD build as opposed to the ESM build) and that will, in turn, load all the locales again.

I hope this helps to someone(options 1 and 2 from comment above, thanks to @patsissons ):
-If you don't have any modules that load moment.js, but your app does, just move to older version
-If you have any modules that also have moment.js in dependencies the above won't help you, you have to modify your webpack config:
{...
resolve: {
...
alias: {
'moment$': 'moment/moment',
}
}
}

-But if you're using react-create-app(like I do) and you need to have working build asap, the only option for you to run 'npm eject', this way you'll have access to webpack config and second option will be available for you.

So I'm sorry but the only method working here was @patsissons' one. https://github.com/moment/moment/issues/4216#issuecomment-335637520

Still

import moment from "moment"
import "moment/locale/fr"
moment.locale("fr")

but adding in webpack

    resolve: {
      alias: {
        'moment$': 'moment/moment',
      }
    },

For some reason, yarn ignored my "resolutions" setting from package.json.
A hacky way to fix it, if you're using moment-timezone is to edit the yarn.lock

[email protected]:
  version "2.18.1"
  resolved "https://registry.yarnpkg.com/moment/-/moment-2.18.1.tgz#c36193dd3ce1c2eed2adb7c802dbbc77a81b1c0f"

"moment@>= 2.9.0":
  version "2.18.1"
  resolved "https://registry.yarnpkg.com/moment/-/moment-2.18.1.tgz#c36193dd3ce1c2eed2adb7c802dbbc77a81b1c0f"

We wait fix 2.19.1....

Could somebody describe what the actual issue is here? Is it simply that webpack doesn't identify the aliased require() ?

@TomMettam 2.19.0 pointed webpack at the es6 code in moment/src/moment,which doesn’t export quite the same interface as moment/moment.

On top of that, the aliased require prevents webpack from packaging all files that could possibly be requested dynamically. Importing the locale files manually would work, except that the obvious choice of importing from moment/locale will load them into a different instance of moment to the one you are actually using.

And if that is all too simple, there are differences in the behavior of different versions of yarn/npm - I happen to be using a version of npm that uses whatever instance of the package is in the root app node_modules folder, but other versions make it harder to override the packag used.

Who decided that changing the export signature in a minor version is a good idea?

Note that this also breaks 3rd party libraries relying on moment

+1

It's not acceptable for a minor upgrade.

I understand the idea behind but please, be more strict about semver.

@darkalor it wasn’t intentional - the export signature in question is only materially different in a limited set of circumstances - the sort of thing that seems to work just fine until it gets exposed to a large number of users.

@darkalor @kud - Regressions happen. Please try to be constructive; everyone is aware that this shouldn't have happened and ranting doesn't help anyone.

@TomMettam Yes it happens but when it happens you can deal with it and by dealing I mean you could upgrade it as major, not minor. That's all. I don't blame anyone, I blame the situation that if someone thinks "oh ok I can upgrade, as it's just a minor", it's rather wrong in this case. ;)

I appreciate all the work done on moment, but I only talk about the unstable situation.

I don't offend anyone, sorry.

@kud not when you have to pick the version number before you know the regression exists. The level of complexity required to support the number of environments moment does is something quite impressive.

@tqc but now you know it, that's the thing. As said previously by @TomMettam, it happens.

Removing or/and adding tags is possible.

Anyway I won't spam anymore here, thank you for the workaround; I added a note for other people not following this issue, in the changelog: https://gist.github.com/ichernev/5f3f4eb02761b4f765a0cccf02cec603#gistcomment-2226628

Have a great day. :)

I'm using yarn as my package manager. For some reason, my yarn.lock file wasn't updating to reflect the change I made in my package.json file. my settings to as @daniesy said. The issue was moment-timezone in my yarn.lock file was still depending on the moment version '^2.18.1'.

To fix it, I ran a yarn cache clean:
yarn cache clean

Then :
yarn install

Hopefully, this should re-generate your yarn.lock file with something along the line of:

[email protected]:
  version "0.5.13"
  resolved "https://registry.yarnpkg.com/moment-timezone/-/moment-timezone-0.5.13.tgz#99ce5c7d827262eb0f1f702044177f60745d7b90"
  dependencies:
    moment ">= 2.9.0"

[email protected], [email protected], "moment@>= 2.9.0", moment@^2.17.1:
  version "2.18.1"
  resolved "https://registry.yarnpkg.com/moment/-/moment-2.18.1.tgz#c36193dd3ce1c2eed2adb7c802dbbc77a81b1c0f" 

Either way, just, check ur yarn lock isn't trying to include a moment version greater than 2.18.1 in any module especially moment-timezone and moment.

I think we have a fix: https://github.com/moment/moment/pull/4232

Best part - I think this will work for both webpack and react native - no sadness!
I've tested in webpack, but would someone using react native test? Note that you will need to run grunt release to get the files built before you can test using an npm linkage.

@maggiepint - Any possibility of that working for Angular-CLI too?

2.19.1 is out, give it a try

Works for me with Webkit.

Thank you @ichernev and everyone involved!

@ichernev : 2.19.1 is working for me.
React 16.
create-react-app.
Not ejected

Moment loaded like this :

import moment from 'moment'
import 'moment/locale/fr'

Thanks for the fix. 👍

Just fired another VSTS build using angular-cli/webpack and redeployed to environment

Everything back to normal

Thanks

I too can confirm this issue is resolved for Angular-CLI v1.4.4. Thank you!

works with typescript/webpack/karma/phantomjs. Thanks for the fix!

To make it work I just included the locale file before moment.
import 'moment/locale/de'; import moment from 'moment';
and later did
moment.locale("de");

seems to still exists in "version": "2.22.2"?

I am also using 2.22.2. the problem still exists

I am using 2.22.2 and seeing the same issue.

As above. Same issue, same version.

Using react native and seeing this error for a release build:

09-04 13:47:38.684 26530 26554 E ReactNativeJS: Requiring unknown module "./locale/de".
09-04 13:47:38.693 26530 26555 E AndroidRuntime: FATAL EXCEPTION: mqt_native_modules
09-04 13:47:38.693 26530 26555 E AndroidRuntime: Process: com.integreat, PID: 26530
09-04 13:47:38.693 26530 26555 E AndroidRuntime: com.facebook.react.common.JavascriptException: Requiring unknown module "./locale/de"., stack:
09-04 13:47:38.693 26530 26555 E AndroidRuntime: c@2:649
09-04 13:47:38.693 26530 26555 E AndroidRuntime: i@2:394
09-04 13:47:38.693 26530 26555 E AndroidRuntime: t@2:324
09-04 13:47:38.693 26530 26555 E AndroidRuntime: vt@882:16755
09-04 13:47:38.693 26530 26555 E AndroidRuntime: Mt@882:17781
09-04 13:47:38.693 26530 26555 E AndroidRuntime: Mn@882:29014
09-04 13:47:38.693 26530 26555 E AndroidRuntime: value@1006:1442
09-04 13:47:38.693 26530 26555 E AndroidRuntime: qn@108:40634
09-04 13:47:38.693 26530 26555 E AndroidRuntime: tr@108:46334
09-04 13:47:38.693 26530 26555 E AndroidRuntime: Nr@108:56528
09-04 13:47:38.693 26530 26555 E AndroidRuntime: Ur@108:56960
09-04 13:47:38.693 26530 26555 E AndroidRuntime: ui@108:61804
09-04 13:47:38.693 26530 26555 E AndroidRuntime: oi@108:61224
09-04 13:47:38.693 26530 26555 E AndroidRuntime: Ue@108:68050
09-04 13:47:38.693 26530 26555 E AndroidRuntime: Ie@108:14248
09-04 13:47:38.693 26530 26555 E AndroidRuntime: We@108:14421
09-04 13:47:38.693 26530 26555 E AndroidRuntime: receiveTouches@108:15235
09-04 13:47:38.693 26530 26555 E AndroidRuntime: value@18:3352
09-04 13:47:38.693 26530 26555 E AndroidRuntime: <unknown>@18:935
09-04 13:47:38.693 26530 26555 E AndroidRuntime: value@18:2621
09-04 13:47:38.693 26530 26555 E AndroidRuntime: value@18:907

2.22.1 works

not working for me still

i cant get 2.18.1 to work for me either

2.22.1 ftw lol

Also noticed a regression in v2.22.2. Rolling back to 2.22.1 fixed it for me.

Hi everyone,

Had the same problem (using Webpack in Laravel). This is not ideal, but a quick fix that worked for me is copying the "locale" folder of the repository alongside the moment.js or moment-with-locale.js file.

Also running into this on v2.22.2.

To make it work I just included the locale file before moment.
import 'moment/locale/de'; import moment from 'moment';
and later did
moment.locale("de");

@mj1856 is it appropriate to re-open this issue or should we create a fresh/new one? I'm also seeing this again. Rolling back to 2.22.1 did not fix it in my case and I had to use @patsissons fix to make webpack happy again.

same

I used allowSyntheticDefaultImports as explained in the doc here: https://momentjs.com/docs/#/use-it/typescript/ and it solved the Cannot find module "./locale" error for me. Note that // @ts-ignore above the import was also needed in my IDE.

same question,moment:'2.18.1'

Is this still advised: import moment from "moment/min/moment-with-locales"; ?

b/c I end up with same (?) error with [email protected] and CRA:

./node_modules/moment/min/moment-with-locales.js
Module not found: Can't resolve './locale' in '/...myproject/node_modules/moment/min'

For all those internet passersby here's a way you can fix it if you build with rollup:

Assuming you're using rollup-plugin-node-resolve:

import commonjs from "rollup-plugin-commonjs";
import nodeResolve from "rollup-plugin-node-resolve";

export default {
    // your config...
    plugins: [
        nodeResolve({
            mainFields: ["module", "jsnext:main", "main"],
            preferBuiltins: false,
        }),
        commonjs({
            include: "node_modules/**",
        }),
    ]
}

moment has a package.json entry for jsnext:main which we can leverage here. to include the locales for us during the build process. Your bundle size will increase, but it'll prevent the console errors 😅

I have this same issue with moment version 2.24.0 in my React application. I guess this issue hasn't been resolved yet.

I have this same issue with moment version 2.24.0 in my React application. I guess this issue hasn't been resolved yet.

Confirming. "moment": "2.24.0". 2.18.1 not solving the issue. Node v12.16.0

Same here, with 2.24.0 for my Preact project.

Still running into this problem please help!!

For me, broken: "moment": "^2.24.0" (resolving to 2.25.0)
Not broken: "moment": "2.24.0"

if you use yarn, add resolutions block to override versions of sub-dependency:

  "resolutions": {
    "moment": "2.24.0"
  }

Use "moment": "2.24.0" and as @rilyu suggested, use resolution. Worked for me.

Reverting to "2.24.0" and removing the "^" worked for me. Looks like their latest version pushed last night and is throwing it off.

Facing issue with"moment": "^2.24.0",.
Uncaught Error: Cannot find module './locale' at

Same issue. Resolved with @enewhuis solution. Thanks!

Same issue. Resolved with @enewhuis solution. Thanks man.

Resolved issue with solution @enewhuis and @rilyu. Thanks.

Same issue. @enewhuis solution to the rescue

For me, broken: "moment": "^2.24.0" (resolving to 2.25.0)
Not broken: "moment": "2.24.0"

Thank you man, I'm struggling with this all day long

Same error and same fix here. I was using moment-timezone.

Before:

    "moment-timezone": "^0.5.28",

After:

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

Same error and same fix here. I was using moment-timezone.

Before:

    "moment-timezone": "^0.5.28",

After:

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

Not working for me with -

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

The above fix doesn't work for me. Node v12.6.0, yarn v1.22.4, react-scripts-ts

Worked for us with

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

and

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

using yarn

It did not work without the resolutions block.

Yep I can confirm adding the resolutions block worked for me. I've no idea why, though...

Thanks @mtogstad !

@mtogstad solution works!

I don't understand how this error could randomly show up and wreck my react app

@bodyfarmer can you please write down steps on how you resolve it?
I'm facing the same issue with react app.

@hasanmehmood In my case it was enough to make a downgrade of the moment dependencies this way in CMD:

npm install [email protected]

Hope this works for you too.

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.

Thanks, @jolalla, I first tried downgrading the version but it didn't work for me. Finally adding the resolutions block showed some magic :)

For others who are are facing a similar issue with their react app. They can do the following 2 steps to resolve it.

In dependencies, replace
"moment": "^2.24.0",
with
"moment": "2.24.0",

And add the resolutions block in package.json

"resolutions": {
    "moment": "2.24.0"
  }

Why is this issue closed!?!??!?

Good time to switch to luxon which is what I did for one project that had a low usage of moment.

Having same problem
"resolutions": {
"moment": "2.24.0"
}
solve my issue.
Thanks.

Anything else I can try? "moment": "2.24.0", and
"resolutions": { "moment": "2.24.0" }
aren't fixing it for me. This is for a Heroku deployment. Any ideas are appreciated.

If by chance anyone else has a monorepo (and/or are using workspaces in package.json), the ‘resolutions’ fix to use the previous moment release 2.24.0 will only work if applied in the root level package.json. In our case, we had to do the following:
{ “resolutions”: { “**/moment”: “2.24.0” } }.

Truly hope moment release 2.25 is fixed properly ASAP!

if you use yarn, add resolutions block to override versions of sub-dependency:

  "resolutions": {
    "moment": "2.24.0"
  }

I've been dealing with with this for an hour. removing ^ helps.

For people using create-react-app and who don't want to eject CRA you can install:

Then create a config-overrides.js at root directory of your project for further overriding.

module.exports = function override(config, env) {
  // do stuff with the webpack config...
  return config;
};

Edit your package.json:

"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-app-rewired test",

Because I use ant design my config-overrides.js looks like this:

const { override, fixBabelImports } = require("customize-cra");
const webpack = require("webpack");

const fixMomentJs = (config) => {
  config.plugins.push(
    new webpack.ContextReplacementPlugin(/^\.\/locale$/, (context) => {
      if (!/\/moment\//.test(context.context)) {
        return;
      }
      // context needs to be modified in place
      Object.assign(context, {
        // include locales
        regExp: /^\.\/(fr|en)/,
        // point to the locale data folder relative to moment's src/lib/locale
        request: "../../locale",
      });
    })
  );
  return config;
};

module.exports = override(
  fixMomentJs,
  fixBabelImports("import", {
    libraryName: "antd",
    libraryDirectory: "es",
    style: "css",
  })
);

But yours will be more simple, something like this I think:

module.exports = function override(config, env) {
  config.plugins.push(
    new webpack.ContextReplacementPlugin(/^\.\/locale$/, (context) => {
      if (!/\/moment\//.test(context.context)) {
        return;
      }
      // context needs to be modified in place
      Object.assign(context, {
        // include locales
        regExp: /^\.\/(fr|en)/,
        // point to the locale data folder relative to moment's src/lib/locale
        request: "../../locale",
      });
    })
  );
  return config;
};

Thanks, @jolalla, I first tried downgrading the version but it didn't work for me. Finally adding the resolutions block showed some magic :)

For others who are are facing a similar issue with their react app. They can do the following 2 steps to resolve it.

In dependencies, replace
"moment": "^2.24.0",
with
"moment": "2.24.0",

And add the resolutions block in package.json

"resolutions": {
    "moment": "2.24.0"
  }

Works for me. Thanks

@hasanmehmood it worked for me, thank you man

Thanks, @jolalla, I first tried downgrading the version but it didn't work for me. Finally adding the resolutions block showed some magic :)

For others who are are facing a similar issue with their react app. They can do the following 2 steps to resolve it.

In dependencies, replace
"moment": "^2.24.0",
with
"moment": "2.24.0",

And add the resolutions block in package.json

"resolutions": {
    "moment": "2.24.0"
  }

Works for me too! Thank you!! :D

Been doing this for hours on my angular 8 app with npm.

  1. I updated from moment 2.17 to 2.24
  2. I added the resolutions as adviced above - "devDependencies": {...},"resolutions": { "moment": "2.24.0" } to package.json
  3. Changed to yarn by doing the following. rm -rf node_modules package-lock.json && yarn
    Thanks a lot guys

I´m pretty desperate... I´m using moment 2.25.1 and changing the "devDependencies": {...},"resolutions": { "moment": "2.25.1" } as above... not working

i am using npm. i am a newbie in react where is devDependencies?
i have changed this but its still not working
Capture

I'm using version "2.25.1" and still happening!

@m4mariaoni - that's because u installed without --save-dev, so you're changing the right part.

If you're still encountering this issue after locking moment to 2.24.0, nuke the moment folder from node_modules and reinstall your packages with npm install --save

Thanks, @jolalla, I first tried downgrading the version but it didn't work for me. Finally adding the resolutions block showed some magic :)

For others who are are facing a similar issue with their react app. They can do the following 2 steps to resolve it.

In dependencies, replace
"moment": "^2.24.0",
with
"moment": "2.24.0",

And add the resolutions block in package.json

"resolutions": {
"moment": "2.24.0"
}

Works for me too! Thank you!! :D!thinks!

Thanks, @jolalla, I first tried downgrading the version but it didn't work for me. Finally adding the resolutions block showed some magic :)

For others who are are facing a similar issue with their react app. They can do the following 2 steps to resolve it.

In dependencies, replace
"moment": "^2.24.0",
with
"moment": "2.24.0",

And add the resolutions block in package.json

"resolutions": {
    "moment": "2.24.0"
  }

Replacing

"moment": "^2.24.0",

with

"moment": "2.24.0",

works for me! The usual,
rm -rf package-lock.json && node_modules

In spite of doing all above measures, still not working...

This is frustrating

Previously created repositories were used moment 2.24.0 with React, and it's still working, but install moment2.24.0 now, doesn't working.... (I've tried all above measures, too)

What is the cause?

I'd imagine you may have a ^2.24.0 instead of 2.24.0 somewhere in a package.json @wlsdud2194?

I don´t, and still the error keeps coming. It´s weird

moment version is 2.24.0 in a package.json instead of ^2.24.0 that since I installed it.

I followed this process, one of the above measures

  1. add resolutions block
  2. remove node_modules directory and reinstall package from yarn.

works for me, but don't know why working...😂

Thank you for your interest : ) @agrohs

Had to downgrade from ^2.25.1 to 2.24.0 to fix the issue

I don´t, and still the error keeps coming. It´s weird

Try running npm ls moment to see if any other module uses moment as well

And add the resolutions block in package.json
"resolutions": { "moment": "2.24.0" }

remove node_modules directory and reinstall package from yarn.

rm -rf package-lock.json && node_modules
 

联系人:刘先生
联系电话:18908205973
传真:028-86676917
E-mail:[email protected]
公司名称:成都首志科技有限公司
公司网址:www.szdma.com
地址:成都市滨江东路162号银海大厦


     成都首志科技有限公司成立于2010年,是成都一家面向中小企业的专业软件定制开发公司,公司主要服务对象是中小企业,提供局域网搭建、网站建设、软件定制解决方案等,并提供从手工业务到电脑业务一整套服务。公司以“诚信为本,服务至上”的原则,突现公司形象;通过我们不懈努力,为客户提供优质服务。

 

We are getting Webpack Error: Cannot find module "./locale" error. We are using 'react-redux-i18n' package in our project which has dependency on 'react-i18nify' and that in turn has dependency on 'moment'. Internally 'moment' is configured to use latest version which is causing this error.

npm

In dependencies
"react": "^16.8.1",
"moment": "2.24.0",
"moment-timezone": "^0.5.28",
"react-moment-proptypes": "^1.5.0",
"react-redux-i18n": "^1.9.3",
"react-redux": "^6.0.0",

Any help would be greatly appreciated.

The latest patch 2.25.3 seems to have fixed it, update the momentjs version to: "moment": "^2.25.3" in package.json
Remove the node_modules folder, and run yarn install or npm install should do it.

For those who've lost some confidence in moment, and don't want their apps to completely break from this issue happening again in the future - I switched a medium-sized codebase over to luxon without too much trouble.

Thank you people...

I changed version to 24.0 and added the "resolutions": { "moment": "2.24.0" } and ran npm install

Then it seemed to work again (working with React, by the way).

Cheers!

The latest patch 2.25.3 seems to have fixed it, update the momentjs version to: "moment": "^2.25.3" in package.json
Remove the node_modules folder, and run yarn install or npm install should do it.

Thank you @jasontll , it works for me

None of these solutions worked for me today.

Been doing this for hours on my react app
with npm.

1. I updated from moment  2.17 to 2.24

2. I added the resolutions as adviced above - `"devDependencies": {...},"resolutions": { "moment": "2.24.0"   }` to package.json

3. Changed to yarn by doing the following. `rm -rf node_modules package-lock.json && yarn`
   Thanks a lot guys

its also worked for guys thanks a lot :-)

I run also in this trouble. I've fixed it with the latest version as of "moment": "^2.26.0"

Was this page helpful?
0 / 5 - 0 ratings

Related issues

RobinvanderVliet picture RobinvanderVliet  ·  3Comments

dbshwang picture dbshwang  ·  3Comments

danieljsinclair picture danieljsinclair  ·  3Comments

chitgoks picture chitgoks  ·  3Comments

dogukankotan picture dogukankotan  ·  3Comments