When updating the dayjs package from 1.8.0 to 1.8.3, I'm getting this error.
I changed my import from import * as dayjs from 'dayjs'; to import dayjs from 'dayjs'; which let me compile, but this error what this result.
Here is the line of code for the error:
const today = dayjs().format('YYYY-MM-DD');
same error here
Bug confirmed. We changed index.d.ts from export = dayjs; to export default dayjs.
TS expert help wanted.
Seems add "esModuleInterop": true, to tsconfig would solve this issue, but should a better way.
What were the benefits of that change? Couple of reasons why to avoid default exports:
https://basarat.gitbooks.io/typescript/docs/tips/defaultIsBad.html
Just released v1.8.4 to revert this change. Please remain using
import * as dayjs from 'dayjs'
in a TypeScript project.
And discussion still welcome
I also saw this issue when tsconfig had this set "allowSyntheticDefaultImports": true,.
@tsiq-jeremy Your Day.js version and TS version, please.
Ahh actually it was a bit more complicated I'm realizing then what I wrote. Basically when you have a library compiling with tsconfig allowSyntheticDefaultImports and then a consuming application that does not use that setting, we got that error. Both the library and consuming application had dayjs as dependencies. Using TS 3.0.3 and dayjs 1.8.9.
I think I may write this all up in a separate issue, but I think a root cause to this is when using tsconfig allowSyntheticDefaultImports you have to then change how you import dayjs from import * as dayjs from 'dayjs'; to import dayjs from 'dayjs'; .
Looks like adding export as namespace dayjs; to the .d.ts file could would allow you to import dayjs both ways. Seems like what React and other libraries do. This allows for importing dayjs the same way regardless of if you have that tsconfig variable set.
This is an issue for me in a Next.js project. On the server side when rendering a React component I get:
TypeError: dayjs__WEBPACK_IMPORTED_MODULE_0___default(...)(...).fromNow is not a function
Works on the client rendering cycle of Next.js but not the Node side.
Adding the following to my tsconfig.json did not fix the issue:
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
Using 1.8.29
Have to use a different date library till this is resolved 馃槩
@danawoodman a reproduction demo repo, please?
@iamkun I figured it out, it was because I imported the "relativeTime" extension in my entry point but didn't import it where I was using it. I just assumed that it would work at the entry point but moving it right next to the code in question fixed it:
import dayjs from "dayjs"
import relativeTime from "dayjs/plugin/relativeTime"
dayjs.extend(relativeTime)
console.log(dayjs(new Date("jan 1, 2020").fromNow())
Most helpful comment
Seems add
"esModuleInterop": true,to tsconfig would solve this issue, but should a better way.