moment: Can't resolve './locale'

Describe the bug

When I configure mainFields of webpack, it outputs errors.

To Reproduce

Steps to reproduce the behavior:

  • Install moment@2.24.0

  • Modify webpack config file

    {
      resolve: {
        mainFields: ['module', 'jsnext:main', 'browser', 'main'],
      },
    }
    
  • Use webpack to build bundles

  • Output some errors on console

    WARNING in ./node_modules/moment/src/lib/locale/locales.js
    Module not found: Error: Can't resolve './locale' in 
    '/home/user/workspace/portal/h5v2/node_modules/moment/src/lib/locale'
    @ ./node_modules/moment/src/lib/locale/locales.js
    @ ./node_modules/moment/src/lib/locale/locale.js
    @ ./node_modules/moment/src/moment.js
    @ ../modules/seed/h5v2/main.js
    

Expected behavior

Webpack build successfully.

Screenshots

image

I modified the highlight line below, webpack build successfully.

image

I think https://github.com/moment/moment/blob/develop/src/lib/locale/locales.js#L78 has some problem.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 16
  • Comments: 42 (2 by maintainers)

Commits related to this issue

Most upvoted comments

Continues with 2.25.1 too.

Downgrading to the previous minor version (2.24.0) works as usual:

npm install --save --save-exact moment@2.24.0

Same problem over here with 2.25.1!

Hello Guys! I faced the same issue from last few hours and finally I have resolved it through changing moment version.

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.

These steps worked for me

npm install --save-dev npm-force-resolutions

add in package.json
 "resolutions": {
    "moment": "2.24.0"
 }

Add to “scripts” node in package.json

"preinstall": "npx npm-force-resolutions"
Type npm install

I’m really surprised this was released with this broken. It breaks everyone’s web pack. Is there an instance for someone where this works properly? Are we not testing web pack builds?

same issue with 2.25.1

2.25.2 should address this. You might need to change your webpack configuration if you don’t want the locale files to be auto-bundled.

i was changed aliasesRequire in moment/min/moment-with-locales.js from

            require('./locale/' + name);
            // because defineLocale currently also sets the global locale, we
            // want to undo that for lazy loaded locales

to

            require('moment/locale/' + name);
            // because defineLocale currently also sets the global locale, we
            // want to undo that for lazy loaded locales

and now works without error No Module ./locale while compiling and on production

facing the same issue. Latest moment version causing issues in many websites . moment team please fix it asap.

Hello Guys! I faced the same issue from last few hours and finally I have resolved it through changing moment version.

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.

Yes, the problem is the version, Thank for your answer.

I’m continuing to see issues this morning. The build succeeds with no errors that I can see, however my application is not loading and the console is reporting:

image

I’m using

"moment": "^2.25.2",
"moment-timezone": "^0.5.28",

I’ve deleted my node_modules and package-lock.json file to do a fresh install

One of workaround is use resolve.alias of webpack to change the entry file when import moment.

This doesn’t really help if the bundle engine is encapsulated inside other packages (e.g. react-script).

Maybe it’s a good idea to move release magic with breaking changes towards a new major release…

@sarazhang123 This is indeed a workaround, not real fix, as far as I am concerned. Consumers of a package shouldn’t really care about tweaking their module bundler’s config in order to make a package work… They should only do it if the package provides some additional features which require changing the config, and it’s not the case of moment.

Just to know, where did you put these lines:

{
  "version": "2.25.1",
   "main": "./moment.js",
   "jsnext:main": "./src/moment.js",
   "module": "./src/moment.js",
}

?

Will wait for a minor or patch version of moment fixing this.