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

Named export 'LazyLoadImage' not found. The requested module 'react-lazy-load-image-component' is a CommonJS module, which may not support all module.exports as named expo rts. #121

Open
aliiadev opened this issue May 27, 2023 · 14 comments

Comments

@aliiadev
Copy link

import { LazyLoadImage } from 'react-lazy-load-image-component';
^^^^^^^^^^^^^
SyntaxError: Named export 'LazyLoadImage' not found. The requested module 'react-lazy-load-image-component' is a CommonJS module, which may not support all module.exports as named expo
rts.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'react-lazy-load-image-component';
const { LazyLoadImage } = pkg;

@yeasinsiam
Copy link

import pkg from 'react-lazy-load-image-component';
const { LazyLoadImage } = pkg;

This will temporary solve this problem

@aliiadev
Copy link
Author

not correct, i tried many times, probably the problem is in production at astro, i used another library to fix it

@yeasinsiam
Copy link

i am using react remix and it seems like working fine.

@Niocas
Copy link

Niocas commented Jul 24, 2023

import pkg from 'react-lazy-load-image-component'; const { LazyLoadImage } = pkg;

This will temporary solve this problem

Does not work for me, images do not load anymore

@Niocas
Copy link

Niocas commented Jul 24, 2023

import pkg from 'react-lazy-load-image-component'; const { LazyLoadImage } = pkg;

This will temporary solve this problem

Okay, it works for prerendering but if I run "npm run dev", it does show any images anymore, only works with prerendering for me

@Weibozzz
Copy link

Weibozzz commented Aug 5, 2023

如何解决?

@aliiadev
Copy link
Author

aliiadev commented Aug 7, 2023

如何解决?

Please use another library

@monolithed
Copy link

Please use another library

Did find the one?

@sisyphusla
Copy link

The same error occurred when I deployed to Vercel

@radandevist
Copy link

with vite remix plugin:

  • does no work:
import pkg from 'react-lazy-load-image-component';
const { LazyLoadImage } = pkg
  • works:
import * as pkg from 'react-lazy-load-image-component';
const { LazyLoadImage } = pkg

🤷‍♂️

@thefathdev
Copy link

Is it working now? I am using react for astro and it show the same error message

@aliiadev
Copy link
Author

Is it working now? I am using react for astro and it show the same error message

used another library to fix it

@aliiadev
Copy link
Author

aliiadev commented Jan 10, 2024

Is it working now? I am using react for astro and it show the same error message

U can use react-lazy-load
`
import React, {useCallback, useEffect, useState} from 'react';
import LazyLoad from 'react-lazy-load';
import type {ImgHTMLAttributes, ReactNode} from "react";
import loadingImage from '../../assets/loading-266.svg'

interface ZuImageProps extends ImgHTMLAttributes {
placeholderZuLazy?: ReactNode;
fallback?: string;
}

const ZuImage = ({placeholderZuLazy, alt, width, height, fallback = '/assets/images/loading-266.svg', src, ...props}: ZuImageProps) => {

const [imgSrc, setImgSrc] = useState<string | undefined>(fallback || src)
const onError = () => setImgSrc(fallback)

const onLoad = useCallback(() => {
    setImgSrc(src);
}, [src]);

useEffect(() => {
    const img = new Image();
    img.src = src as string;
    img.addEventListener("load", onLoad);
    return () => {
        img.removeEventListener("load", onLoad);
    };
}, [src, onLoad]);

return (
    <LazyLoad className={'flex items-center justify-center'} width={width} height={height}>
    {/*// @ts-ignore */}
    <img src={imgSrc ? imgSrc : "/assets/images/chodienlogo.jpg"}  onError={onError} style={{maxHeight: '100%', objectFit: 'contain'}} {...props} alt={alt} width={width} height={height}/>
</LazyLoad>

)
}
export default ZuImage
`

@alinasrabadi
Copy link

Vite has stricter handling for CommonJS modules, so you can fix this by using dynamic imports:

const [LazyLoadImage, setLazyLoadImage] = useState(null);

useEffect(() => {
  import('react-lazy-load-image-component').then((pkg) => {
    setLazyLoadImage(() => pkg.LazyLoadImage);
  });
}, []);

return LazyLoadImage ? <LazyLoadImage src="..." alt="Image" />

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

9 participants