dearn44
dearn44

Reputation: 3432

I get the "default is not a constructor" error when trying to use `glidejs`

I have the following code:

import Glide from "@glidejs/glide";

const SectionSlider = () => {
const UNIQUE_CLASS = "random_string"
let MY_GLIDEJS = useMemo(() => {
    return new Glide(`.${UNIQUE_CLASS}`, {
      perView: itemPerRow,
      gap: 32,
      bound: true,
      breakpoints: {
        1280: {
          perView: itemPerRow - 1,
        },
        1024: {
          gap: 20,
          perView: itemPerRow - 1,
        },
        768: {
          gap: 20,
          perView: itemPerRow - 2,
        },
        640: {
          gap: 20,
          perView: itemPerRow - 3,
        },
        500: {
          gap: 20,
          perView: 1.3,
        },
      },
    });
  }, [UNIQUE_CLASS]);

  useEffect(() => {
    setTimeout(() => {
      MY_GLIDEJS.mount();
    }, 100);
  }, [MY_GLIDEJS, UNIQUE_CLASS]);

return (
    <div className={`${UNIQUE_CLASS} flow-root`}>
    ...
    </div>
)};

When rendering this component it throws this error:

TypeError: _glidejs_glide__WEBPACK_IMPORTED_MODULE_3__.default is not a constructor

Additionally the packages I am using (related with this issue) are:

"dependencies": {
    "@glidejs/glide": "^3.6.0",
    "react": "18.2.0",
...
"devDependencies": {
    "@types/glidejs__glide": "^3.6.0",

What is my mistake exactly?

Upvotes: 0

Views: 1127

Answers (2)

Kuntu
Kuntu

Reputation: 1

This will make the navigations of the images work for you

import Glide, { Controls, Breakpoints } from '@glidejs/glide/dist/glide.modular.esm'

When mounting your Glide instance pass these additional parameters to enable controls

new Glide('.glide').mount({ Controls, Breakpoints })

You can find reference at https://glidejs.com/docs/setup/

Upvotes: 0

Phil
Phil

Reputation: 164895

Looks like Glide uses the now defunct module declaration (see this answer).

From their package.json

"module": "dist/glide.esm.js",

According to Webpack documentation, this should still work but something else seems wrong. I'd try raising a bug.

To work around this for now, you can use the following

import Glide from "@glidejs/glide/dist/glide.modular.esm" // may need ".js"

Note that @types/glidejs__glide only defines types for:

  • dist/glide.js, and
  • dist/glide.modular.esm.js

Upvotes: 0

Related Questions