Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'./locale' path not found in moment/src/lib/locale/locales.js #4505

Closed
SunnySnail opened this issue Mar 19, 2018 · 54 comments
Closed

'./locale' path not found in moment/src/lib/locale/locales.js #4505

SunnySnail opened this issue Mar 19, 2018 · 54 comments

Comments

@SunnySnail
Copy link

WARNING in ./node_modules/moment/src/lib/locale/locales.js
Module not found: Error: Can't resolve './locale' in 'D:\project-fed\ecloud\client-web\node_modules\moment\src\lib\locale'

I found that there is something wrong with the function below

function loadLocale(name) {
    var oldLocale = null;
    // TODO: Find a better way to register and load all the locales in Node
    if (!locales[name] && (typeof module !== 'undefined') &&
            module && module.exports) {
        try {
            oldLocale = globalLocale._abbr;
            var aliasedRequire = require;
            aliasedRequire('./locale/' + name);
            getSetGlobalLocale(oldLocale);
        } catch (e) {}
    }
    return locales[name];
}

aliasedRequire('./locale/' + name);, should it be ../locale/ ?

@RikkiGibson
Copy link

I see this same warning when I use webpack to bundle up moment. Any idea when this error was introduced?

@michDostal
Copy link

I have the same warning using angular-cli any solution?

@maxmantz
Copy link

maxmantz commented May 9, 2018

Having the same issue. Do you know of any workarounds / fixes?

@ghost
Copy link

ghost commented Jul 10, 2018

Same issue - looks like a path issue with './locale' vs '../locale'. Is anyone looking into this bug?

@svk31
Copy link

svk31 commented Jul 11, 2018

#2979

Looks like it's been the case for years, why they won't fix it I have no idea. I just ran into this as well when trying to use jsnext:main in webpack instead of main.

My work-around to keep using jsnext:main in webpack for the other packages that have properly transpiled es6 code: use an alias to force webpack to use moment.js directly as it is specified in pkg.main

... resolve: { alias: { moment$: path.resolve(root_dir, "node_modules/moment/moment.js") } }

@happilymarrieddad
Copy link

The above didn't work for me but following this tutorial helped fix it for me.

https://alligator.io/angular/custom-webpack-config/

@garbinmarcelo
Copy link

Is there anything new about this? I'm using Laravel Mix... same thing here...

@happilymarrieddad
Copy link

I do

import * as moment from 'moment';

and that works.

@garbinmarcelo
Copy link

I do

import * as moment from 'moment';

and that works.

inside the webpack? I am thinking about how to do this with Laravel Mix..

@happilymarrieddad
Copy link

@marcelogarbin oh sorry I haven't messed with Laravel since version 4. I apologize but .I don't know how to make it work with laravel mix.

@yaasinhamidi
Copy link

Just import like below and it's work :

import moment from 'moment/moment';

@Spriithy
Copy link

Spriithy commented Feb 8, 2020

@yaasinhamidi's solution did the trick

@lid3rs
Copy link

lid3rs commented Feb 12, 2020

I do

import * as moment from 'moment';

and that works.

Where should this line be put?

@robtesch
Copy link

robtesch commented May 1, 2020

Experiencing this same issue for the first time after updating to 2.25.0. I am also using Laravel Mix (note, i never experienced this issue on any previous versions).

@MaximusBaton
Copy link

my workaround is to get back to 2.24.0

@bhatvikrant
Copy link

I am facing the same issue after updating

@bhatvikrant
Copy link

Until it is fixed, rolling back to v2.24.0 solves this issue.

@danlsaur
Copy link

danlsaur commented May 1, 2020

Rolling back to v.2.24.0 worked for me as well.

@jelanijohn
Copy link

for anyone else experiencing this issue, i had to also force the versions of moments that other packages in my node_modules/ folder pulled in as dependencies to v2.24.0 as well

using yarn, adding this line to package.json sorted things for me

"resolutions": {
		"**/moment": "2.24.0"
	},

@MohsinGhani
Copy link

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

  • "moment": "2.24.0",
  • "moment-timezone": "^0.5.28",

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.

@sachins81
Copy link

Even if you have, following block. The package-lock.json will create a new version i.e. 2.5.1. So change it to "moment": "2.24.0" till the issue is addressed.
"dependencies" {
"moment": "^2.24.0", ----------------> "moment": "2.24.0"
},

@nitindevelopermca
Copy link

i rolled back to 2.24.0 but still having same warning while using node

@hugozap
Copy link

hugozap commented May 2, 2020

It's weird I started getting this error today after re installing dependencies, the suggested fix of ignoring the locale plugins with webpack didn't work. What worked was leaving the version fixed at 2.24.0 as suggested by @sachins81 . I wonder if something relevant changed in recent commits? @ichernev

@hugozap
Copy link

hugozap commented May 2, 2020

i rolled back to 2.24.0 but still having same warning while using node
@nitindevelopermca Make sure it's "2.24.0" and not "^2.24.0". (Worked for me after changing it)

@nitindevelopermca
Copy link

I just rolled back to "^2.18.1" and everything seems working fine

@svnty
Copy link

svnty commented May 2, 2020

Same issue using react, moment isnt a dependancy of my project so I can't roll it back

@NebSehemvi
Copy link

NebSehemvi commented May 2, 2020

Fantastic fix :) solved a major headache with this

Mediocre fix, if someone doesn't have antd as dependency directly.

I've encountered the same problem but I don't have momentjs in dependencies. But antd does.
And npm can't work with resolutions.

The only solution for me right now is downgrading antd.
I can't see proper solution which wouldn't be including extra package to make things work.

@imsam67
Copy link

imsam67 commented May 2, 2020

I have the same issue in my React app which, for some reason, started to show up out of the blue. I've been using moment in this app forever and had never noticed this error. It actually became an error in my case as my React app wouldn't compile because of this issue.

I then tried import moment from 'moment/moment'; which fixed the compilation issue but I still get the following warning everytime I run the app.

./node_modules/moment/src/lib/locale/locales.js
Module not found: Can't resolve './locale' in 'C:\Users\myname\source\repos\myapp\node_modules\moment\src\lib\locale'

The only saving grace is that my React app, at least, compiles and runs fine.

@ayarhlaine
Copy link

I have the same issue with create-react-app (typescript template) too.I tried import moment from 'moment';.Then I got the error :
./node_modules/moment/src/lib/locale/locales.js Module not found: Can't resolve './locale' in 'D:\MyName\Learning\ProjectName\node_modules\moment\src\lib\locale'

@MohsinGhani
Copy link

@devayarhlaine I have post a temporary solution in above comments please check it.

Its was a moment new version issue.

@BePo65
Copy link

BePo65 commented May 3, 2020

Same s above:
I have the same issue with "Cannot find module 'moment'" too.
Using the solution with "import * as moment from 'moment/moment' solved one issue.
But then I got the error :
./node_modules/moment/src/lib/locale/locales.js Module not found: Can't resolve './locale' in 'D:\MyName\Learning\ProjectName\node_modules\moment\src\lib\locale'
and downgrading isn't a solution (in my opinion :-).

Got this error when upgrading from angular 8 to angular 9.1.
Upgrading to moment 2.25.1 solved the issue #5486 ("TS2307: Cannot find module 'moment'. "), but the locale error remained.

@liangsuno
Copy link

liangsuno commented May 3, 2020

For those using the command "yarn create react-app ", the fix is to edit node_modules/react-scripts/config/webpack.config.js to add the alias like below.

moment$: 'moment/moment.js'

Insert the above line into the alias block at line number 311.

alias: {
  // Support React Native Web
  // https://www.smashingmagazine.com/2016/08/a-glimpse-into-the-future-with-react-native-for-web/
  'react-native': 'react-native-web',
  // Allows for better profiling with ReactDevTools
  ...(isEnvProductionProfile && {          'react-dom$': 'react-dom/profiling',
  'scheduler/tracing': 'scheduler/tracing-profiling',        }),
  ...(modules.webpackAliases || {}),        
  moment$: 'moment/moment.js'
},

Another way to solve this is to replace moment.js with Day.js. Follow the instructions in this url to use react-app-rewired >> https://ant.design/docs/react/use-with-create-react-app. Modify package.json according to the instruction. Then create a config-overrides.js in the root folder like below. This has the benefit of shrinking the js from 231.11kb to 11.11kb according to this https://github.com/ant-design/antd-dayjs-webpack-plugin.

const { override, addWebpackPlugin } = require('customize-cra');    
const AntdDayjsWebpackPlugin = require('antd-dayjs-webpack-plugin');

module.exports = override(
        addWebpackPlugin(new AntdDayjsWebpackPlugin())
);

@lid3rs
Copy link

lid3rs commented May 3, 2020

Just import like below and it's work :

import moment from 'moment/moment';

Fantastic fix!!!

@alisonmoura
Copy link

Just import like below and it's work :

import moment from 'moment/moment';

Worked for me

@ApacheEx
Copy link

ApacheEx commented May 3, 2020

loooks like root of issue was caused by "module" field in package.json (9ce89e7)

here is PR to fix it #5503

@imsam67
Copy link

imsam67 commented May 3, 2020

Yup, removing "module": "./src/moment.js", from package.json made the warning go away for me. BTW, mine is a React app -- create-react-app and I'm using "moment": "^2.25.1". Even though I was using the latest version, I was still getting this error but updating the package.json seems to fix the issue. Thanks @ApacheEx

@tryn0
Copy link

tryn0 commented May 3, 2020

I have fix this error, at least for me.

I went to node_modules/moment/src/lib/locale/
Modified the locales.js file, ctrl+f and search './locale'+name line.
Change it to './'+name.
I don't know why is pointing to same folder and '/.locale' + name, this is why the error says can't resolve ./locale

To be honest, I copied the original line and commented just above, if in the future an error shows up.

If it helps, thumbs up. This kind of errors are the worse...

msorens added a commit to chef/automate that referenced this issue May 3, 2020
Running `ng build` after updating all the packages revealed a problem
with just one: moment going from 2.24.0 => 2.25.1
```
WARNING in ./node_modules/moment/src/lib/locale/locales.js
Module not found: Error: Can't resolve './locale' in '/Users/msorens/code/go/src/github.com/chef/automate/components/automate-ui/node_modules/moment/src/lib/locale'
```

The issue was encountered by many:
moment/moment#4505 (comment)

The particular comment in the thread I linked to has a suggested workaround,
though I had to modify it to get it to work.

Using it exactly as is in that thread generated this error on every `moment` use:
```
This module is declared with using 'export =', and can only be used with a default import when using the 'allowSyntheticDefaultImports' flag.
```

But I found that I could retain `import *...`
instead of `import moment...` to solve
the original issue without encountering the follow-up issue.

This commit, then, makes that simple change in 57 files.
One file imported `moment` slightly differently so adjusted that as well.

Signed-off-by: michael sorens <msorens@chef.io>
@BoazKG93
Copy link

BoazKG93 commented May 4, 2020

Having the same problem, reverted to 2.24 until a fix is available. I can't edit the module files, as i'm building on an automatic server, so waiting for an official release.

@MDzyga
Copy link

MDzyga commented May 4, 2020

I have the same issue with 2.24 and 2.25.1 version
WARNING in ./node_modules/moment/src/lib/locale/locales.js

When I open browser console, I see this error:
TypeError: Cannot read property 'defineLocale' of undefined at locales.min.js:1

msorens added a commit to chef/automate that referenced this issue May 4, 2020
Running `ng build` after updating all the packages revealed a problem
with just one: moment going from 2.24.0 => 2.25.1
```
WARNING in ./node_modules/moment/src/lib/locale/locales.js
Module not found: Error: Can't resolve './locale' in '/Users/msorens/code/go/src/github.com/chef/automate/components/automate-ui/node_modules/moment/src/lib/locale'
```

The issue was encountered by many:
moment/moment#4505 (comment)

The particular comment in the thread I linked to has a suggested workaround,
though I had to modify it to get it to work.

Using it exactly as is in that thread generated this error on every `moment` use:
```
This module is declared with using 'export =', and can only be used with a default import when using the 'allowSyntheticDefaultImports' flag.
```

But I found that I could retain `import *...`
instead of `import moment...` to solve
the original issue without encountering the follow-up issue.

This commit, then, makes that simple change in 57 files.
One file imported `moment` slightly differently so adjusted that as well.

Signed-off-by: michael sorens <msorens@chef.io>
@MatteoGioioso
Copy link

We are having the same issue with moment 2.22

@kiknadze
Copy link

kiknadze commented May 6, 2020

Just import like below and it's work :

import moment from 'moment/moment';

Where should this line be put?

@yaasinhamidi
Copy link

yaasinhamidi commented May 6, 2020

Just import like below and it's work :
import moment from 'moment/moment';

Where should this line be put?

In the file that you want use momentjs on ecmascript like below:

import moment from 'moment/moment';
(() => {
const now = moment(moment.now());
console.log(now);
})

@marwahaha
Copy link
Member

2.25.3 should fix this issue.
Please open another issue if you still have challenges.
Or, you can try Stack Overflow for more individual questions.

@wizardion
Copy link

This alias with the WebPack config fixes an issue for me:

resolve: {
  extensions: ['.js', '.ts'],
  alias: {
    './locale': 'moment/locale'
  }
}

@bleuscyther
Copy link

If you are using moment-timezone:
moment/moment-timezone#837

@GuilhermeSSeabra
Copy link

i fix this with this:

import moment from 'moment'; import 'moment/locale/pt-br';

it works, and the locale is set to 'pt-br', and all others languages works too.

Zkffkah added a commit to ilovefibos/fibostoolkit that referenced this issue Aug 20, 2020
forember pushed a commit to forember/theia-electron that referenced this issue Mar 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests