moment: moment with locales: "./locale" not found
I’m getting this warning when using webpack:
WARNING in ./~/moment/min/moment-with-locales.js
Module not found: Error: Cannot resolve 'file' or 'directory' ./locale in \node_modules\moment\min
@ ./~/moment/min/moment-with-locales.js 271:16-43
any clues?
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 105
- Comments: 48 (5 by maintainers)
Commits related to this issue
- use `empty-module` and `ContextReplacementPlugin` to exclude moment locales(via https://github.com/moment/moment/issues/2979#issuecomment-189899510) — committed to Raiyee/EasyHi by JounQin 8 years ago
- use `empty-module` and `ContextReplacementPlugin` to exclude moment locales(via https://github.com/moment/moment/issues/2979#issuecomment-189899510) — committed to Raiyee/EasyHi by JounQin 8 years ago
- Merge pull request #4042 from ovangle:fix-4031 [bugfix] Make auto locale loading for node not mess webpack, fixes #4031, #2979, #3872 — committed to moment/moment by ichernev 7 years ago
- Fix moment version to 2.18.1 b/c newer version created locales problem. https://github.com/moment/moment/issues/2979 — committed to johnnyoshika/jellypic-dashboard-from-redux-starter-kit by johnnyoshika 7 years ago
- Attempting to fix moment error that only occurs in >=2.19.0. https://github.com/moment/moment/issues/2979 — committed to bcgov/MyGovBC-MSP by acoard 7 years ago
- Fix moment version to 2.18.1 b/c newer version created locales problem. https://github.com/moment/moment/issues/2979 — committed to johnnyoshika/jellypic-dashboard-from-redux-starter-kit by johnnyoshika 7 years ago
- Fix moment version to 2.18.1 b/c newer version created locales problem. https://github.com/moment/moment/issues/2979 — committed to johnnyoshika/jellypic-dashboard-from-redux-starter-kit by johnnyoshika 7 years ago
- :house: Ingore moment locale in dev server https://github.com/moment/moment/issues/2979#issuecomment-287675568 — committed to LiskHQ/lisk-desktop by slaweet 5 years ago
- CONFIG update import statement for moment to avoid error Module not found: Can't resolve './locale' in '/usr/src/app/node_modules/moment/src/lib/locale' https://github.com/moment/moment/issues/2979#is... — committed to bargar/react-timeline-gantt by bargar 4 years ago
I used the ignore module with good results. This might help someone:
The simplest solution how to convince webpack not to look for locales already included in the moment-with-locales.js is to force him to load something else instead. This worked for me:
install empty module (it literary contains nothing)
npm install --save-dev empty-module
and then configure ContextReplacementPlugin as followsnew webpack.ContextReplacementPlugin(/\.\/locale$/, 'empty-module', false, /js$/)
Hopefully this helps.
I tried @miguelrincon suggestion above but it doesn’t work. i.e. It doesn’t throw the error during the build, but the same error is thrown in the browser console log.
After looking around, I found this and it works perfectly!
https://github.com/afc163/react-boilerplate/commit/61ec8a19df0fcb56d407b795cb6c87141e0e14a7
This problem has appeared for me in moment.js 2.19.0. If you need to get your application working right now a rollback should do it. For example:
npm install moment@2.18.0 --save
dear, I solve it! src/lib/locale/locales.js in line 61, 61 require(‘./locale/’ + name); change to be: 61 require(‘./’ + name); “moment with locales: “./locale” not found” will be solve.
the suggested workaround in “plugins” of webpack.config.js
did not work
To confirm @francisrod01 's solution, I have changed my import, so that instead of
import moment from 'moment/min/moment-with-locales'
I changed to
import moment from 'moment'
import 'moment/min/locales'
And this solved for me. Hope it helps.
With create-react-app, below import worked fine for me
import * as moment from "moment/moment.js"
solved! …v2.19.1
I confirm that sticking to 2.18 fixes the issue immediately.
The fix is simple …
find any thing like this:
and replace it with this
I found the alias solution posted above by @M1chaelTran to work well. In case someone using Laravel Mix is wondering how to apply it, your
webpack.mix.js
should look similar to this at the top:Same here. Using React 16, installed moment 2.19 and got errors. Reverted back to 2.18, no errors.
How To Fix in Hybrid Apps Ionic 3.5 with Angular 4 where we have no webpack.config file?
This solution worked for me.
just had similar problem when using momentjs via
npm
I found that onlocales.js
it refers to./locale/
I assume that line trying to find language localization. CMIIWMeanwhile the locale files are located on
../../locale/
fromlocale.js
by changing that line intorequire ('../../locale/' + name);
I resolved the issue but I couldn’t editnode_modules
directly like this on my serverwill try webpack way mentioned above later
EDIT: I was running this on windows, it looks like path problem. I tried running moment on linux and it works just fine
We also just had a problem when upgrading to moment 2.19 in an app also using react and webpack. Reverting to 2.18 fixed it.
Awesome. It works!
See #1435. Workaround at the end. Thanks.
add above to package.json and re yarn. ok for me
@miguelrincon works fine!
The problem mentioned above is about
require('./locale' + name)
statement included in the file min/moment-with-locales.js. It is not necessary anymore because all locales are already included in this file itself.Unfortunately webpack tries to resolve all require statements - therefore this warning.
Here is the fix for this issue:
File: "moment\src\lib\locale\locales.js " line 56: “./locale” needs to be changed to “…/…/locale”
Alexandra
Downgrading to 2.18 does not solve the issue for me.
i use angular cli. moment worked but when i want export my component with npm run packagr ==>
Compiling to FESM15 ‘locale’ is not exported by ‘node_modules\moment\src\moment.js’
BUILD ERROR Cannot call a namespace (‘moment’) Error: Cannot call a namespace (‘moment’)
somebody can help me ?
UPDATE: I found a solution.
The right way to import :
import * as moment_ from 'moment'; const moment = moment_;
and in ng-package.json:
{ "lib": { "externals": { "moment": "moment" } } }
By me was the structure false, the externals was not inside lib, so packgr was looking for lib.externals and couldn’t find moment.
Ng-packgr & moment
I’m using the latest version, right now it’s 2.22.2.
While I’m trying to use ng-packagr and pack my project, I get this error. Same as : @vZanchiV
‘locale’ is not exported by ‘node_modules\moment\src\moment.js’
BUILD ERROR Cannot call a namespace (‘moment’) Error: Cannot call a namespace (‘moment’)
In project it’s importer in this way :
import * as moment from ‘moment’;
I found a workaround that didn’t went well, I tried importing it like :
import * as momentImported from ‘moment’; const moment = momentImported;
After this method I could export my project with ng-packagr but then I failed importing my project in another project, with this problem
Module not found: Error: Can’t resolve ‘./locale’ in 'C:\development\angularTest\angulartest\node_modules\modulename
I’m still seeing the issue in v2.19.1
However, @M1chaelTran alias trick did work for me.
@wushuang5112 you cant edit a node_module , so your solution would required to add moment in to the repository , which is not good
It seems no need to use
empty-module
, just change it to null, it still works in ContextReplacementPluginnew webpack.ContextReplacementPlugin(/\.\/locale$/, null, false, /js$/)
See https://momentjs.com/docs/#/-project-status/
I have moment
2.24.0
and this worked for me finally:Almost the same as fix of @aaronkrohn , but instead of
request: '../../locale',
I had to userequest: '../locale',
.Thank you @jeff3dx This fixed my issue!