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

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.<anonymous> (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.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 42
  • Comments: 69 (4 by maintainers)

Commits related to this issue

Most upvoted comments

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.

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

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 ?

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:

└─┬ antd@4.2.0
  ├── moment@2.25.1 
  └─┬ rc-picker@1.4.10
    └── moment@2.25.1  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.

2.25.3 is out without module.

Any news from the devs?

i’ve had to revert to “2.24.0”, moment can’t find ‘./locale’

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.

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/)
...
]

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

Why are these issues closed…this is wreaking havoc

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!?!?!

Handling of require was reverted to 2.24.0 behavior.

Fixed in 2.25.1.

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.

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.

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 ^