Moment: Uncaught Error: Cannot find module './locale'

Created on 1 May 2020  ·  69Comments  ·  Source: moment/moment

Describe the bug
I get this error on chrome console after I build the production optimized build

Uncaught Error: Cannot find module './locale'
at 2.d0944550.chunk.js:1
at Module. (2.d0944550.chunk.js:1)
at f ((index):1)
at Module.837 (main.091f08fc.chunk.js:1)
at f ((index):1)
at Object.540 (main.091f08fc.chunk.js:1)
at f ((index):1)
at a ((index):1)
at Array.e [as push] ((index):1)
at main.091f08fc.chunk.js:1

This is my package.json

{
  "name": "sheetgo-front-end",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@allpro/react-router-pause": "^1.1.3",
    "@material-ui/core": "^4.4.2",
    "@material-ui/icons": "^4.5.1",
    "@material-ui/lab": "^4.0.0-alpha.26",
    "@material-ui/styles": "^4.4.1",
    "@material/icon-button": "^3.1.0",
    "@material/react-button": "^0.15.0",
    "@material/react-card": "^0.15.0",
    "@material/react-icon-button": "^0.15.0",
    "@material/react-material-icon": "^0.15.0",
    "@material/react-select": "^0.15.0",
    "@material/react-tab": "^0.15.0",
    "@mdi/js": "^3.4.93",
    "@mdi/react": "^1.1.0",
    "@stripe/react-stripe-js": "^1.1.0",
    "@stripe/stripe-js": "^1.2.0",
    "@svgr/webpack": "^4.1.0",
    "apexcharts": "^3.8.6",
    "axios": "^0.19.2",
    "browser-info": "^1.2.0",
    "chart.js": "^2.7.2",
    "classnames": "^2.2.6",
    "color-sort": "^0.0.1",
    "connected-react-router": "^6.5.2",
    "countup.js": "^1.9.3",
    "cra-append-sw": "^2.7.0",
    "filepond": "4.1.0",
    "filepond-plugin-file-validate-size": "2.1.1",
    "filepond-plugin-file-validate-type": "1.2.2",
    "form-serialize": "^0.7.2",
    "fuse.js": "^3.4.2",
    "history": "^4.10.1",
    "inputmask": "^4.0.0",
    "is-mobile": "^2.0.0",
    "jquery": "^3.3.1",
    "jquery-ui-sortable-npm": "^1.0.0",
    "js-cookie": "^2.2.0",
    "lodash": "^4.17.10",
    "markdown-to-jsx": "^6.10.3",
    "material-components-web": "^3.1.0",
    "material-ui-chip-input": "^1.0.0",
    "md5": "^2.2.1",
    "memoize-one": "^5.0.4",
    "moment": "^2.22.2",
    "moment-timezone": "^0.5.27",
    "onecolor": "^3.1.0",
    "p-queue": "^6.1.1",
    "payform": "^1.2.2",
    "promise-throttle": "^1.0.0",
    "prop-types": "^15.7.0",
    "query-string": "^6.5.0",
    "react": "^16.8.1",
    "react-apexcharts": "^1.3.3",
    "react-dom": "^16.8.1",
    "react-draggable": "4.2.0",
    "react-filepond": "7.0.1",
    "react-google-authorize": "^1.0.4",
    "react-hotjar": "^2.0.0",
    "react-id-swiper": "^1.6.8",
    "react-intl": "^2.5.0",
    "react-modal-video": "^1.2.3",
    "react-redux": "^7.1.1",
    "react-resizable": "^1.8.0",
    "react-router": "^5.1.2",
    "react-router-dom": "^5.1.2",
    "react-router-last-location": "^2.0.1",
    "react-scripts": "^2.1.4",
    "react-sizeme": "^2.5.2",
    "react-stripe-elements": "^4.0.0",
    "react-text-mask": "^5.4.3",
    "react-virtualized-auto-sizer": "^1.0.2",
    "react-vis-network": "^1.0.0",
    "react-window": "^1.8.1",
    "recompose": "^0.30.0",
    "redux": "^4.0.4",
    "redux-react-session": "^2.4.0",
    "redux-saga": "^1.1.1",
    "redux-thunk": "^2.3.0",
    "socket.io-client": "^2.3.0",
    "sort-by": "^1.2.0",
    "tether": "^1.4.4",
    "throttle-debounce": "^2.1.0",
    "url-loader": "^1.0.1"
  }

If I build locally with yarn for the dev version it works

To Reproduce
Steps to reproduce the behavior:
Build an app with those requirements. Should get that error on console.

Expected behavior
A clear and concise description of what you expected to happen.

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: [e.g. iOS]
  • Browser [e.g. chrome, safari]
  • Version [e.g. 22]

Smartphone (please complete the following information):

  • Device: [e.g. iPhone6]
  • OS: [e.g. iOS8.1]
  • Browser [e.g. stock browser, safari]
  • Version [e.g. 22]

Moment-specific environment

  • The time zone setting of the machine the code is running on
  • The time and date at which the code was run
  • Other libraries in use (TypeScript, Immutable.js, etc)

Please run the following code in your environment and include the output:

console.log((new Date()).toString())
console.log((new Date()).toLocaleString())
console.log((new Date()).getTimezoneOffset())
console.log(navigator.userAgent)
console.log(moment.version)

Additional context
Add any other context about the problem here.

Most helpful comment

Same issue,

Fixed by removing carret sign before version number. Package.json entry now looks like:
"moment": "2.24.0"

It looks like a bug from the new version.

All 69 comments

Exactly. I just faced this error and saw that they just pushed a new version hours ago.

if you use yarn, add resolutions block to package.json to override versions of sub-dependency, it work for me

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

Same here!

Also 2.24.0 still has the warning: ./node_modules/chart.js/node_modules/moment/src/lib/locale/locales.js Critical dependency: the request of a dependency is an expression

Just reproduce this problem

Same too!

  • 1

Same issue,

Fixed by removing carret sign before version number. Package.json entry now looks like:
"moment": "2.24.0"

It looks like a bug from the new version.

Tonight I installed Jest and it created a problem and I was instructed to remove any yarn.lock and package.lock files along with my modules folder and do yarn install or npm install. When I did yarn install I received the error mentioned in this thread - the page that appeared said the error was a webpack error and pointed to moment-timezone. When I removed my lock files , module folder and reinstalled using npm install (instead of yarn install) - everything worked.In short when I used a different package manager command it changed the outcome.

Please remove ^ in package.json this way its worked for me 👍

strange such incident happened early this week due to is-promise library there also issue like ^ need to remove. I believe people will now more or less do for all package.json strict version instead of ^

Tried removing the caret from package.json and running + rm -rf node_modules + yarn and I still get this problem when running webpack

I am using moment-timezone, and it defaults to 2.25. removing caret makes no difference

Same here!

This works for me, I am using create-react-app.
// in webpack.config.js

plugins: [
...
// new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
// replace the line above with below, regex to match the locale you want to use
new webpack.ContextReplacementPlugin(/moment[\/\\]locale/,  /(en|zh-cn)\.js/)
...
]

It appears there's an issue with their latest version. I had to revert back to "2.24.0" without the "^" and that allowed my sites to build.

Same problem here with moment version 2.25.0 in one of my gatsby-based projects:

Cannot find module './locale'                              


> 1 | import { Moment } from './constructor';                                                                                                 
    | ^                                                                                                                                       
  2 |                                                                                                                                         
  3 | var proto = Moment.prototype;                                                                                                           
  4 |                                                                                                                                         


  WebpackError: Cannot find module './locale'                                                                                                 

  - prototype.js:1 webpackMissingModule                                                                                                       
    node_modules/moment/src/lib/moment/prototype.js:1:1                                                                                       

  - prototype.js:1 Module../node_modules/moment/src/lib/moment/prototype.js                                                                   
    node_modules/moment/src/lib/moment/prototype.js:1:1                                                                                       

  - moment.js:1 Module../node_modules/moment/src/lib/moment/moment.js                                                                         
    node_modules/moment/src/lib/moment/moment.js:1:1                                                                                          

  - moment.js:1 Module../node_modules/moment/src/moment.js                                                                                    
    node_modules/moment/src/moment.js:1:1

With moment 2.24.0 there is no such issue.

Can someone provide a minimal (non)-working example so this can be investigated?

i've had to revert to "2.24.0", moment can't find './locale'

this works perfectly for me : https://github.com/moment/moment/issues/4216#issuecomment-622453248

Can someone provide a minimal (non)-working example so this can be investigated?

Hey @ichernev - please find attached. Just run npm install, and npm start :)

issue5484.zip

Handling of require was reverted to 2.24.0 behavior.

Fixed in 2.25.1.

I've updated to the version 2.25.1, but I still got the error.

prototype.js:1 Uncaught Error: Cannot find module './locale'
    at webpackMissingModule (prototype.js:1)
    at Module../node_modules/moment/src/lib/moment/prototype.js (prototype.js:1)
    at __webpack_require__ (bootstrap:782)
    at fn (bootstrap:150)
    at Module../node_modules/moment/src/lib/moment/moment.js (moment.js:1)
    at __webpack_require__ (bootstrap:782)
    at fn (bootstrap:150)
    at Module../node_modules/moment/src/moment.js (moment.js:1)
    at __webpack_require__ (bootstrap:782)
    at fn (bootstrap:150)
    at Module../src/Menu.js (Home.js:186)
    at __webpack_require__ (bootstrap:782)
    at fn (bootstrap:150)
    at Module../src/App.js (Add.js:157)
    at __webpack_require__ (bootstrap:782)
    at fn (bootstrap:150)
    at Module../src/index.js (index.css?02e3:45)
    at __webpack_require__ (bootstrap:782)
    at fn (bootstrap:150)
    at Object.0 (serviceWorker.js:135)
    at __webpack_require__ (bootstrap:782)
    at checkDeferredModules (bootstrap:45)
    at Array.webpackJsonpCallback [as push] (bootstrap:32)
    at main.chunk.js:1

But maybe I'm doing something wrong. Can anybody confirm that this error has disappeared in 2.25.1 ?

Same issue for me and moment-timezone. Ive tried forcing 2.24.0, but still same issue since im guessing moment-timezone uses the latest?

Still getting the error

@strange1120 is [email protected]?
If you use yarn, try yarn why moment to show installed deps.

@ichernev Sorry, I can confirm 2.25.1 throws the same error. Using the app I uploaded previously, and a clean node_modules/package-lock.json, I can see the version is 2.25.1 but still throws.

Why are these issues closed...this is wreaking havoc

Yeah me too. I thought it was my Docker

Removed suggestion - not worthwhile in 2020 :( @ichernev any update yet?

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;
};

For people who have access to Webpack you can add the following entry to you plugin array in your Webpack config:

    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",
      });
    })

Until the fix will be released, as was mentioned by @saadzr, you can temprorary just assign moment to "2.24.0" version. Remove carret: "^".

For those who does not know how to do it. Find the packages which use moment: npm ls moment. You will probably see:

└─┬ [email protected]
  ├── [email protected] 
  └─┬ [email protected]
    └── [email protected]  deduped

So inside antd/package.json and rc-picker/package.json in dependencies:

"moment": "^2.24.0", -> "moment": "2.24.0"

And run npm i TWICE. Do not know why need twice, but it only works after twice.

@talgat-ruby This implies to edit the node_modules folder that is not pushed on repositories, people who clone the project will have to do each time, this is a not a good workaround I think.
For instance we work with multiple collaborators and we can't told them to do this each time they clone a repository.
The best way to fix this until a fix is released is to configure Webpack as mentioned upper.

@nathanagez this is a temprorary fix. Antd team will probably release official fix soon. And yes you are right you can not rely on it.

@talgat-ruby I explained how to configure Webpack, see upper, so you can push the workaround on your repository.

i can confirm that @talgat-ruby suggestion fixed the problem for me.

@rilyu Your suggestion worked for me!

I am using moment-timezone and didn't specified a version for moment.
It seems then, during the install, yearn installs the latest version of moment as a dependency -> [email protected] and it's not compatible.

Indeed adding
"resolutions": { "moment": "2.24.0" }
solves the problem.

big thanks

Any news from the devs?

Getting this error using MaterialUI DatePicker. Tried rolling back manually to 2.24.0 but no luck for me. Hope to see this resolved soon. I guess I will use the native TextField type="date" for now.

Same issue,

Fixed by removing carret sign before version number. Package.json entry now looks like:
"moment": "2.24.0"

It looks like a bug from the new version.

This works for me! Tnx

I am having the same problem i have deleted node_modules folder then changed the version of moment to 2.24.0 and then again I installed, the issue persists.
When I check rc-calendar is using 2.25.1 something like this. I tried editing there to and did yarn install still problem persists. Please help I'm working on an important project

@faizhameed Try to remove node-modules folder.
Remove your package-lock.json as well ( yarn.lock also if you're using yarn ).

Add this to your package.json after dependencies: {
...
},
resolutions: { "moment": "2.24.0" }

It Should force rc-datepicker to use moment 2.24

Same problem here after upgrading to 2.25.0 and 2.25.1.
The only solution that worked for me is downgrade to 2.24.0

Unfortunately I have the same problem. For some reason, the solution in the package.json is not working for me.
resolutions: { "moment": "2.24.0" }

For now I'm going to downgrade like @pcarballeda

For me fixing the version to "moment": "2.24.0" and adding resolutions: { "moment": "2.24.0" } temporarily solved the problem.

Module not found: Error: Can't resolve './locale' in 'D:\Downloads\Telegram\ildamnode_modules\rc-pickernode_modules\momentsrc\lib\locale'

How can this be overcome?

"moment": "2.24.0"

"dependencies": {
"antd": "^4.2.0",
"axios": "^0.19.2",
"classnames": "^2.2.6",
"cross-env": "^7.0.2",
"dva": "^2.4.1",
"dva-model-extend": "^0.1.2",
"less-vars-to-js": "^1.3.0",
"libphonenumber-js": "^1.7.50",
"lodash": "^4.17.15",
"lodash.clonedeep": "^4.5.0",
"moment": "2.24.0",
"nprogress": "^0.2.0",
"path-to-regexp": "^6.1.0",
"prop-types": "^15.7.2",
"query-string": "^6.12.1",
"react": "^16.13.1",
"react-currency-format": "^1.0.0",
"react-custom-scrollbars": "^4.2.1",
"react-dom": "^16.13.1",
"react-helmet": "^6.0.0",
"react-perfect-scrollbar": "^1.5.8",
"svg-sprite-loader": "^4.2.1",
"umi-plugin-locale": "^2.11.5"
},

Downgrading to 2.24.0 worked for me. Remove the ^ with 2.24.0.
Also manually updated all the moment dependencies to 2.24.0 in package-lock.json as well.

Having the issue with the 2.24, trying to put the "resolutions"... but I didn't update to 2.25, what's wrong? It was working on Thursday.

Downgrading to 2.24.0 worked for me. Remove the ^ with 2.24.0.
Also manually updated all the moment dependencies to 2.24.0 in package-lock.json as well.

Thanks worked! There is a problem with Yarn. Worked with Npm

Until the fix will be released, as was mentioned by @saadzr, you can temprorary just assign moment to "2.24.0" version. Remove carret: "^".

For those who does not know how to do it. Find the packages which use moment: npm ls moment. You will probably see:

└─┬ [email protected]
  ├── [email protected] 
  └─┬ [email protected]
    └── [email protected]  deduped

So inside antd/package.json and rc-picker/package.json in dependencies:

"moment": "^2.24.0", -> "moment": "2.24.0"

And run npm i TWICE. Do not know why need twice, but it only works after twice.

Working! <3

yarn remove + yarn add moment installed version 2.25.2 which builds fine for me

yarn remove + yarn add moment installed version 2.25.2 which builds fine for me

Wouldn’t that be a problem in this version?

@Shakxzod-Namazbaev I think the original problem was with version 2.25.0, some people reported that the same problem happens in 2.25.1, others have mentioned that downgrading to 2.24 works for them. In my case version 2.25.2 seems to work, so maybe downgrading is not needed

@Shakxzod-Namazbaev I think the original problem was with version 2.25.0, some people reported that the same problem happens in 2.25.1, others have mentioned that downgrading to 2.24 works for them. In my case version 2.25.2 seems to work, so maybe downgrading is not needed

So you don't have a problem with version 2.25.2 right now, right?

@Shakxzod-Namazbaev exactly, 2.25.2 works fine for me.

I can confirm - moment 2.25.2 works also for me.

I think 2.25.2 is OK in standalone form but in packaged-managed form, it's still broken (webpack in my case where moment is a dependency of moment-timezone).

2.24.0 is still the one for people using webpack and other moment-dependant packages.

ver. 2.25.2 works for me in package-managed form - webpack configured in a gatsby-based project (ver. 2.25.0 didn't):

relevant package.json lines:

dependencies: {
    ...
    "moment": "^2.25.2",
    ...
}

I'm not using moment-timezone though.

So crazy that this Issue report is still sitting as Closed with so many people affected and the only "solution" so far is to downgrade to an old version!?!?!

@ichernev Thank you! Updating to 2.25.2 fixes this issue for me!

--- a/front/package.json
+++ b/front/package.json
@@ -5,7 +5,7 @@
   "dependencies": {
-    "moment": "^2.22.2",
+    "moment": "^2.25.2",

OK. I praised too early. Although with moment ver. 2.25.2 webpack is able to produce working bundle without errors, but importing locale doesn't work anymore - looks like it's being ignored (perhaps this has something to do with sideEffects / tree shaking?).

E.g.:

import moment from "moment"
import "moment/locale/pl"

// set global 'moment' locale (polish)
moment.locale("pl")

// ... somewhere later
console.log(moment().format("DD MMMM YYYY HH:mm:ss"))

With version 2.25.2 output is 04 May 2020 16:38:15, which is incorrect, as it should be
04 maja 2020 16:38:58.

So, version 2.24.0 is still the last working one.

I faced the same issue, removing carrot from the version fixed this.

Like this in your package.json file --- "moment": "2.24.0"

Version : 2.24.0

Well, I was testing locally, and I can't really tell why, but if the is a package.json property module then it's broken. If there is only jsnext:main it is working. This relates to locale files not being loaded properly, while still included in the bundle.

So this "modern" module property that suppersedes jsnext:main is not just a rename, or an alt name. It has some hidden agenda that is not well advertised.

2.25.3 is out without module.

@ichernev - thanks! I can confirm version 2.25.3 works (webpack is able to produce bundle without errors and including/setting locale works as expected).

Fixes in 2.25.3 for me.

Same issue,

Fixed by removing carret sign before version number. Package.json entry now looks like:
"moment": "2.24.0"

It looks like a bug from the new version.

After that works!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

slavafomin picture slavafomin  ·  3Comments

BCup picture BCup  ·  3Comments

vbullinger picture vbullinger  ·  3Comments

M-Zuber picture M-Zuber  ·  3Comments

Shoroh picture Shoroh  ·  3Comments