iamkun / dayjs

⏰ Day.js 2kB immutable date-time library alternative to Moment.js with the same modern API
https://day.js.org
MIT License
46.71k stars 2.28k forks source link

[Question] Class Dayjs defined in source and type definition but 'new Dayjs()' throws #1541

Open BePo65 opened 3 years ago

BePo65 commented 3 years ago

Looking at the source, there is a definition for a class named Dayjs with a constructor. Even in the type definition files I can see this class in the namespace dayjs. And in line 48 of 'dayjs/esm/index.js' return new Dayjs(cfg); is used.

But when I try to use const demo = new dayjs.Dayjs() or const demo = new dayjs() all I get when running the program is an error that says TypeError: dayjs.Dayjs is not a constructor no matter if I use

So the question is: what am I doing wrong; where is my misinterpretation of the source / type definition.

Information

BePo65 commented 3 years ago

IMHO it's a babel theme. The (esm) source clearly has a class with a constructor. But it seams that node will not recognize the code generated by babel as a class.

So for the esm version in dayjs/esm I am asking myself, why do you let babel transform esm to esm? But probably I just don't get the point :-)

Stuart88 commented 4 months ago

Same problem happening for me now too.

New React project made via create-react-app (which uses webpack), with antd as the UI library. Their datepicker uses dayjs for some reason, and it is not working because of this exact problem - "dayjs is not a constructor"

Specifically:

dayjs__WEBPACK_IMPORTED_MODULE_14___default(...).Dayjs is not a constructor
TypeError: dayjs__WEBPACK_IMPORTED_MODULE_14___default(...).Dayjs is not a constructor
    at DrawRouteDetails (http://localhost:3000/static/js/bundle.js:6912:40)
    at renderWithHooks (http://localhost:3000/static/js/bundle.js:151075:22)
    at updateFunctionComponent (http://localhost:3000/static/js/bundle.js:153955:24)
    at beginWork (http://localhost:3000/static/js/bundle.js:155667:20)
    at HTMLUnknownElement.callCallback (http://localhost:3000/static/js/bundle.js:140671:18)
    at Object.invokeGuardedCallbackDev (http://localhost:3000/static/js/bundle.js:140715:20)
    at invokeGuardedCallback (http://localhost:3000/static/js/bundle.js:140772:35)
    at beginWork$1 (http://localhost:3000/static/js/bundle.js:160636:11)
    at performUnitOfWork (http://localhost:3000/static/js/bundle.js:159884:16)
    at workLoopSync (http://localhost:3000/static/js/bundle.js:159807:9)
Snote100024 commented 2 months ago

I had the same issue. Use const demo = dayjs(); instead of const demo = new dayjs();