reactjsmomentjsvite

Could not resolve require("./locale/**/*")


I am using Vite+React. I am getting error while running npm run dev.

"[ERROR] Could not resolve require("./locale/**/*") node_modules/moment-with-locales-es6/node_modules/moment/min/moment-with-locales.min.js:1:16645: 1 │ ...e&&module&&module.exports)try{b=Bf._abbr,require("./locale/"+a),$a(b)}catch(a){}return Ff[a]}function $a(a,b){v..."

I am using below versions of the packages.

I also added below code in vite.config.js file.

 resolve: {
    alias: {`your text`
        'moment$': 'moment/min/moment.with-locales.js',
    },
},

How to solve this problem?

I tried with multiple versions of Moment.js like 2.18.1, 2.25.2 etc.


Solution

  • I was able to solve this error after changing import syntax from

    import moment from "moment"
    

    to

    import * as moment from "moment/moment"