ericnograles / browser-image-resizer

A tiny browser-based library to downscale and/or resize images using canvas
MIT License
98 stars 42 forks source link

Nextjs ReferenceError: self is not defined #87

Closed opeodedeyi closed 5 months ago

opeodedeyi commented 10 months ago

Everything works well till i deploy the app and the code is building on Vercel or Netlify and then i get the ReferenceError: self is not defined

below is my import code and what is compressed

'use client';

import "./forminput.css";
import { useState, useRef } from 'react';
import { readAndCompressImage } from 'browser-image-resizer';

const SingleImageUploadInput = ({ children }) => {
    const readImage = async (file) => {
        const config = {
            quality: 0.7,
            maxWidth: 1920,
            maxHeight: 1080,
            mimeType: 'image/webp',
            debug: true,
        };

        try {
            const resizedImageFile = await readAndCompressImage(file, config);
            const reader = new FileReader();

            return new Promise((resolve, reject) => {
                reader.onloadend = () => {
                    resolve(reader.result);
                };
                reader.onerror = reject;
                reader.readAsDataURL(resizedImageFile);
                let resizedImgSize = resizedImageFile.size;
                setImageSize(convertFileSize(resizedImgSize));
            });
        } catch (error) {
            console.log('Failed to resize the image:', error);
        }
    };
TomerH0525 commented 8 months ago

const { readAndCompressImage } = (await import('browser-image-resizer')); try implementing this fix should help,

ericnograles commented 5 months ago

Thanks for the reply @TomerH0525 -- this is related to #77. I will add some guidance on the docs for static site usage per that issue.