Open chrisvidal opened 3 years ago
I have a similar issue in my backend app. I'm importing this extensions:
import * as timezone from 'dayjs/plugin/timezone';
import * as utc from 'dayjs/plugin/utc';
dayjs.extend(utc);
dayjs.extend(timezone);
dayjs.extend(relativeTime);```
and I having this error:
`TypeError: Cannot read property '$i' of undefined
at Function.w.extend (/home/rayser/api-somos/somos-api/node_modules/dayjs/dayjs.min.js:1:6381)
`
I just realized that the error ocurrer just when extending dayjs.extend(relativeTime)
same for relativeTime or updateLocale
same here for "utc" on nodejs
this worked for me:
import * as dayjs from 'dayjs';
import * as utc from 'dayjs/plugin/utc';
import * as timezone from 'dayjs/plugin/timezone';
dayjs.extend(utc);
dayjs.extend(timezone);
dayjs.tz.setDefault('Europe/Prague');
@chrisvidal I also had the same issue and found a different way of extending; Add dayjsplugin before the plugin name you require.
<script>
dayjs.extend(window.dayjs_plugin_isSameOrAfter);
</script>
this worked for me:
import dayjs from 'dayjs'
import * as isSameOrAfter from 'dayjs/plugin/isSameOrAfter '
dayjs.extend(isSameOrAfter)
// import { isSameOrAfter } from 'dayjs/plugin/isSameOrAfter '
// dayjs.extend(isSameOrAfter)
As the doc of dayjs say you can do so :
import * as dayjs from 'dayjs'
dayjs().format()
Have trouble importing Day.js?
If your tsconfig.json contains the following config, you must do the default import workflow import dayjs from 'dayjs'
:
{
"compilerOptions": {
"esModuleInterop": true, <-- make sure to have that to do import dayjs from 'dayjs'
"allowSyntheticDefaultImports": true,
}
}
If you don't have these config above, the default import won't work, and you'll continue to have to use import * as dayjs from 'dayjs'
some typescript doc about esModuleInterop to understand the issue: https://www.typescriptlang.org/tsconfig#esModuleInterop
I also made this mistake
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dayjs/1.11.11/dayjs.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dayjs/1.11.11/plugin/advancedFormat.min.js"></script>
</head>
<body>
<script>
dayjs.extend(window.isSameOrAfter);
</script>
</body>
</html>
Describe the bug
dayjs.min.js:1 Uncaught TypeError: Cannot read properties of undefined (reading '$i')
at O.extend (dayjs.min.js:1:7047)
at test.html:12:13
Information
Describe the bug
Expected behavior no error
Information