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.99k stars 2.3k forks source link

Cant add isSameOrAfter plugin: Cannot read property '$i' of undefined #1494

Open chrisvidal opened 3 years ago

chrisvidal commented 3 years ago

Describe the bug

<script src="https://cdnjs.cloudflare.com/ajax/libs/dayjs/1.10.4/dayjs.min.js"></script>
 <script src="https://cdnjs.cloudflare.com/ajax/libs/dayjs/1.10.4/plugin/isSameOrAfter.min.js"></script>
<script>
dayjs.extend(window.isSameOrAfter);
</script>
Uncaught TypeError: Cannot read property '$i' of undefined
    at Function.v.extend (dayjs.min.js:1)
    at register:724

Expected behavior no error

Information

raysercast1 commented 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)
`
raysercast1 commented 3 years ago

I just realized that the error ocurrer just when extending dayjs.extend(relativeTime)

ambrt commented 3 years ago

same for relativeTime or updateLocale

razab commented 3 years ago

same here for "utc" on nodejs

cilf commented 3 years ago

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');
josephcondon commented 1 year ago

@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>
chendahao commented 1 year ago

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)

billalouaali commented 1 year ago

As the doc of dayjs say you can do so :

Import and use in your TypeScript file

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

tsconfig.js

{
  "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

AIDeepx commented 5 months ago

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