moment: Webpack Error: Cannot find module "./locale" after updating to 2.19.0
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’
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 224
- Comments: 153 (7 by maintainers)
Links to this issue
Commits related to this issue
- Pin moment version https://github.com/moment/moment/issues/4216 — committed to fyndiq/fyndiq-ui by deleted user 7 years ago
- Merge pull request #4226 from ichernev:revert-package-json-es6 [critical] Revert "jsnext:main -> module (#3904)", fixes #4216 — committed to moment/moment by ichernev 7 years ago
- Downgrade moment lib in admin client (due to bug). https://github.com/moment/moment/issues/4216 — committed to zentrope/GoWebl by zentrope 7 years ago
- fixing issue with moment see: https://github.com/moment/moment/issues/4216 — committed to marinels/webrx-react by patsissons 7 years ago
- Pin moment to 2.18.1 until upstream issue is fixed: https://github.com/moment/moment/issues/4216 — committed to TerriaJS/terriajs by meh9 7 years ago
- Merge pull request #2686 from TerriaJS/pin-moment-2.18.1 Pin moment to 2.18.1 until upstream issue is fixed: https://github.com/moment/moment/issues/4216 — committed to TerriaJS/terriajs by kring 7 years ago
- TODO: Pin moment js to 2.18.1 until issue is fixed: https://github.com/moment/moment/issues/4216 — committed to m3db/m3ctl by deleted user 7 years ago
- fixing issue with moment see: https://github.com/moment/moment/issues/4216 — committed to marinels/webrx-react by patsissons 7 years ago
- Fixing moment to 2.18.1 Trying to fix this issue: https://github.com/palantir/blueprint/issues/1847 Using the advice suggested in: https://github.com/moment/moment/issues/4216 — committed to tnrich/blueprint by tnrich 6 years ago
- unlock moment The bug that caused us to lock it down has been resolved. https://github.com/moment/moment/issues/4216 — committed to mxenabled/mx-react-components by cerinman 6 years ago
- Update moment to fix vulnerability (#774) * Update moment to fix vulnerability https://github.com/moment/moment/pull/4326 * unlock moment The bug that caused us to lock it down has been reso... — committed to mxenabled/mx-react-components by cerinman 6 years ago
- TODO: Pin moment js to 2.18.1 until issue is fixed: https://github.com/moment/moment/issues/4216 — committed to m3db/m3 by deleted user 7 years ago
- TODO: Pin moment js to 2.18.1 until issue is fixed: https://github.com/moment/moment/issues/4216 — committed to m3db/m3 by deleted user 7 years ago
- TODO: Pin moment js to 2.18.1 until issue is fixed: https://github.com/moment/moment/issues/4216 — committed to m3db/m3 by deleted user 7 years ago
- Set moment locale Set moment locale before using to fix console warning: Cannot find module "./locale" See: https://github.com/moment/moment/issues/4216 — committed to flibbertigibbet/climate-change-components by flibbertigibbet 5 years ago
- fix(buid): moment js warning to in build Attempted the fixed in clockface and localy to no avail. Instead, just filtering the warnings out. https://github.com/moment/moment/issues/4216 — committed to influxdata/influxdb by 121watts 5 years ago
- fix(buid): moment js warning to in build Attempted the fixed in clockface and localy to no avail. Instead, just filtering the warnings out. https://github.com/moment/moment/issues/4216 — committed to influxdata/influxdb by 121watts 5 years ago
- fix(portal): plugin moment build failed Ref: https://github.com/moment/moment/issues/4216#issuecomment-335613586 Signed-off-by: Rajesh Rajendran <rjshrjndrn@gmail.com> — committed to rjshrjndrn/sunbird-portal by rjshrjndrn 3 years ago
- fix(portal): plugin moment build failed Ref: https://github.com/moment/moment/issues/4216#issuecomment-335613586 Signed-off-by: Rajesh Rajendran <rjshrjndrn@gmail.com> — committed to rjshrjndrn/sunbird-portal by rjshrjndrn 3 years ago
- Merge pull request #4225 from ichernev:revert-require-rename [critical] Revert require rename, fixes #4216 — committed to horus68/moment by ichernev 7 years ago
@patricksteenks : yes. Fix like this in you package.json
"moment": "2.18.1"
For me, broken: “moment”: “^2.24.0” (resolving to 2.25.0) Not broken: “moment”: “2.24.0”
solved via webpack aliasing:
Same issue on Angular-CLI built project
if you use yarn, add resolutions block to override versions of sub-dependency:
2.19.1 is out, give it a try
Repent, repent, doomsday is here!
Worked for us with
and
using yarn
It did not work without the resolutions block.
I am using 2.22.2 and seeing the same issue.
I ended up getting past this by changing:
import * as moment from 'moment';
to:
import * as moment from 'moment/moment';
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)
package.json (issue in general)
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
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):
i can confirm. package.json
@renedx just this line enough
@darkalor @kud - Regressions happen. Please try to be constructive; everyone is aware that this shouldn’t have happened and ranting doesn’t help anyone.
Is this still advised:
import moment from "moment/min/moment-with-locales";
?b/c I end up with same (?) error with
moment@2.24.0
and CRA:+1
It’s not acceptable for a minor upgrade.
I understand the idea behind but please, be more strict about semver.
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 runyarn install
ornpm install
should do it.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
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.
Okay, here’s where we’re at:
Confirmed.
@hasanmehmood In my case it was enough to make a downgrade of the moment dependencies this way in CMD:
npm install moment@2.24.0
Hope this works for you too.
Same here, with
2.24.0
for my Preact project.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.
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 to2.18.1
without the^
, and now it will not install anything higher than 2.18.1 until we know 2.19.x is safeI´m pretty desperate… I´m using moment 2.25.1 and changing the “devDependencies”: {…},“resolutions”: { “moment”: “2.25.1” } as above… not working
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
I have this same issue with moment version 2.24.0 in my React application. I guess this issue hasn’t been resolved yet.
Good time to switch to luxon which is what I did for one project that had a low usage of moment.
Why is this issue closed!?!??!?
@mtogstad solution works!
I don’t understand how this error could randomly show up and wreck my react app
To make it work I just included the locale file before moment.
import 'moment/locale/de'; import moment from 'moment';
and later didmoment.locale("de");
Same here.
Same in an Angular 4 application. Just downgraded to 2.18.1 (strict version number in package.json)
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!! 😄!thinks!
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!
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.@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.
@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.
npm install moment@2.18.1
as a temporary fix so you don’t have to deal with updatingpackage.json
multiple times. This will probably get fixed soon, then you can donpm i
.I added a metro.config.js file after upgrading, which didn’t allow the files to be loaded correctly. Removed the file and it works again.
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
works for me, but don’t know why working…😂
Thank you for your interest : ) @agrohs
Works for me too! Thank you!! 😄
Thank you man, I’m struggling with this all day long
@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.
seems to still exists in “version”: “2.22.2”?
@ichernev : 2.19.1 is working for me. React 16. create-react-app. Not ejected
Moment loaded like this :
Thanks for the fix. 👍
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:
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.
@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.
We wait fix 2.19.1…
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.
@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
@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.
@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?
MCVE: https://github.com/mj1856/moment-webpack-test
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.
I can confirm this is also happening!
With 2.29.1 dynamic import of locales still does not work with webpack and the create-react-app config (worked with 2.24).
A workaround is to add a new Javascript file in your project that just imports the one locale and then dynamically import that file.
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.
@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
Been doing this for hours on my angular 8 app with npm.
"devDependencies": {...},"resolutions": { "moment": "2.24.0" }
to package.jsonrm -rf node_modules package-lock.json && yarn
Thanks a lot guys@hasanmehmood it worked for me, thank you man
The above fix doesn’t work for me. Node v12.6.0, yarn v1.22.4, react-scripts-ts
same
Also noticed a regression in v2.22.2. Rolling back to 2.22.1 fixed it for me.
i cant get 2.18.1 to work for me either
works with typescript/webpack/karma/phantomjs. Thanks for the fix!
I too can confirm this issue is resolved for Angular-CLI v1.4.4. Thank you!
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
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 callmoment(date).format('ddd')
@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.
In our project we’re using
yarn 1.0.2
,create-react-app
, and these deps inpackage.json
Although we’re using![screen shot 2017-10-10 at 3 53 43 pm](https://user-images.githubusercontent.com/4350583/31407558-4ebb6a76-add3-11e7-9ef6-8a60649486ef.png)
"moment": "2.18.1"
, we’re still gettingIs there a workaround of the issue for yarn?
Can confirm: