Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
BePo65 opened this issue Jun 19, 2021 · 2 comments

Comments

@BePo65
Copy link
Contributor

BePo65 commented Jun 19, 2021

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
Copy link
Contributor Author

BePo65 commented Jul 8, 2021

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
Copy link

Stuart88 commented Apr 28, 2024

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants