dayjs icon indicating copy to clipboard operation
dayjs copied to clipboard

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

Open BePo65 opened this issue 4 years ago • 3 comments

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

  • node.js with commonjs (const dayjs = require('dayjs'); or
  • node.js with esm (import dayjs from 'dayjs';'); or
  • typescript (angular 11) with esm (import dayjs from 'dayjs/esm';');

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

Information

  • Day.js Version [e.g. v1.10.5]
  • OS: windows 10
  • node 14.17.1

BePo65 avatar Jun 19 '21 04:06 BePo65

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 :-)

BePo65 avatar Jul 08 '21 05:07 BePo65

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)

Stuart88 avatar Apr 28 '24 18:04 Stuart88

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

Snote100024 avatar Jul 06 '24 12:07 Snote100024