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

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

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 22
  • Comments: 54 (1 by maintainers)

Commits related to this issue

Most upvoted comments

Just import like below and it’s work :

import moment from ‘moment/moment’;

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.

I do

import * as moment from 'moment';

and that works.

https://github.com/moment/moment/issues/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") } }

my workaround is to get back to 2.24.0

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

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

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

You can run this commands for now.

npm install moment@2.24.0 --save

I had the same case and when I search somehow antd was using moment as a dependency and i hope the moment issue will be closed in next release while i saw they released a new version so please first try the latest version which is 2.25.1 then try to add moment as a dependency.

Thank You!

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” },

I am facing the same issue after updating

I do

import * as moment from 'moment';

and that works.

Where should this line be put?

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

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

Thanks . Its work fine. just import the line bellow: import * as moment from ‘moment/moment’;

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…

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

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

loooks like root of issue was caused by "module" field in package.json (https://github.com/moment/moment/commit/9ce89e7fea881b39be23b8c0646f7ef7817985d9)

here is PR to fix it https://github.com/moment/moment/pull/5503

For those using the command “yarn create react-app <app name>”, 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())
);

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

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)

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

I do

import * as moment from 'moment';

and that works.

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