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

Most upvoted comments

I used the ignore module with good results. This might help someone:

    // /app/js/webpack.config.js
    plugins: [
        
        // Fixes warning in moment-with-locales.min.js 
        //   Module not found: Error: Can't resolve './locale' in ...
        new webpack.IgnorePlugin(/\.\/locale$/)
    ]

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 follows new 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

\\ webpack.js

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

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

new webpack.ContextReplacementPlugin(/moment[\\\/]locale$/, /^\.\/(en|de|cz|eu)$/)

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:

import moment from 'moment/src/moment';

and replace it with this

import * as moment from 'moment';

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:

let mix = require('laravel-mix');

mix.webpackConfig({
    resolve: {
        alias: {
            moment$: 'moment/moment.js'
        }
    }
});

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?

        new webpack.ContextReplacementPlugin(/^\.\/locale$/, (context) => {
            if (!/\/moment\//.test(context.context)) return;

            Object.assign(context, {
                regExp: /^\.\/\w+/,
                request: '../../locale', // resolved relatively
            });
        }),

This solution worked for me.

just had similar problem when using momentjs via npm I found that on locales.js it refers to ./locale/ I assume that line trying to find language localization. CMIIW

Meanwhile the locale files are located on ../../locale/ from locale.js by changing that line into require ('../../locale/' + name); I resolved the issue but I couldn’t edit node_modules directly like this on my server

will 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.

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

add above to package.json and re yarn. ok for me

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

WARNING in ./~/moment/src/lib/locale/locales.js
Module not found: Error: Can't resolve './locale' in '/project/node_modules/moment/src/lib/locale'
 @ ./~/moment/src/lib/locale/locales.js 56:12-46
 @ ./~/moment/src/lib/locale/locale.js
 @ ./~/moment/src/moment.js
 @ dll reactBoilerplateDeps

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 ContextReplacementPlugin

new webpack.ContextReplacementPlugin(/\.\/locale$/, null, false, /js$/)

I have moment 2.24.0 and this worked for me finally:

    // Fixes warning in moment-with-locales.min.js
    //   Module not found: Error: Can't resolve './locale' in ..
    new webpack.ContextReplacementPlugin(/^\.\/locale$/, context => {
      if (!/\/moment\//.test(context.context)) return

      Object.assign(context, {
        regExp: /^\.\/\w+/,
        request: '../locale', // resolved relatively
      })
    })

Almost the same as fix of @aaronkrohn , but instead of request: '../../locale', I had to use request: '../locale',.

Thank you @jeff3dx This fixed my issue!