Donaldcwl / browser-image-compression

Image compression in web browser
MIT License
1.31k stars 161 forks source link

WebpackError: ReferenceError: File is not defined #39

Closed apradipta10 closed 4 years ago

apradipta10 commented 4 years ago

I am using browser image compression for my website using gatsby. I just import this, import imageCompression from 'browser-image-compression';

when I start to gatsby develop everything is normal, but when I tried to gatsby build, i just got error this.

Screen Shot 2020-01-29 at 14 37 43

Donaldcwl commented 4 years ago

It is related to this issue: https://github.com/gatsbyjs/gatsby/issues/309

In gatsby development, react components are only run in the browser where window.File is defined. When building, Gatsby renders these components on the server where window.File is not defined. One workaround is to use a try-catch block warping the import of this library.

browser-image-compression@v1.0.8 will support SSR. By that time, you won't need to use a workaround.