woltapp/blurhash

ES module import issue

andyzaharia opened this issue ยท 19 comments

Hi guys, newbie JS developer here.

I've been using this framework in a nodejs app for quite a while and decided to update the server to use ES modules. All went great with all the dependencies that I had in the project but when I got to import the encode func, nodejs complains about the package being a CommonJS module, even thou... I don't think thats true.

import { encode } from "blurhash"; - this ends up with a _SyntaxError: Named export 'encode' not found. The requested module 'blurhash' is a CommonJS module... _ erorr

and if I do this:

import blurhash from "blurhash"; - I end up with a SyntaxError: Unexpected token 'export' error.

Does anyone have any idea what this happens?

Thanks a lot and sorry of this is a stupid question.

Later edit: This problems seems to be happening only with the 2.0.0 version, the 1.1.5 works fine.

I have the exact same issue.
I have tried importing dist/esm/index.js directly, etc., but no matter how I try, I can't solve the issue.

This seems to be a problem in blurhash package.json.

To fix it, I had to edit node_modules/blurhash/package.json and added the line:
"type": "module"

Then you can use object deconstruction to import blurhash

import { encode } from 'blurhash'

I'll look into this, when I have some time.

Confirmed, similar issue here. I'm writing a Vite plugin that leverages blurhash and get:

import { encode } from "blurhash";
         ^^^^^^
SyntaxError: Named export 'encode' not found. The requested module 'blurhash' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'blurhash';
const { encode } = pkg;

The issue is detailed above but I thought provide the exact issue as seen with my bundler.

I reverted to 1.1.3 which has the correct export. Using 2.0.0 is blocked until the export detailed above is resolved.

Thanks for maintaining this neat project.

Hi all ๐Ÿ‘‹๐Ÿผ

Please try out 2.0.1 and let me know if that resolves your issues.

@Thisen am using 2.0.1 and same issue.

Looks like is still loading the CJS version in Vite, giving error: 'module' is not defined or the does not provide export one above.

@Thisen am using 2.0.1 and same issue.

Looks like is still loading the CJS version in Vite, giving error: 'module' is not defined or the does not provide export one above.

Could you provide me with a reproduction?

@Thisen sure...
https://github.com/kaption-co/blurhash-repro

Note that this repro excludes 'blurhash' from optimizing the default CJS file to ESM...

@Thisen sure... https://github.com/kaption-co/blurhash-repro

Note that this repro excludes 'blurhash' from optimizing the default CJS file to ESM...

Thank you for the reproduction! It helped me to resolve the issue and it should work in 2.0.2. Please try it out.

@Thisen sure... https://github.com/kaption-co/blurhash-repro
Note that this repro excludes 'blurhash' from optimizing the default CJS file to ESM...

Thank you for the reproduction! It helped me to resolve the issue and it should work in 2.0.2. Please try it out.

#210 pointed out in exports field order, try 2.0.3. ๐Ÿ’ช๐Ÿผ

@mrtnzlml I see you had some issues, could you try out 2.0.3? :)

@Thisen this is resolved for me in latest version.

Awesome! Closing.

Hi @Thisen! ๐Ÿ‘‹ There is still some breaking change in the patch release, as you can see in this PR: adeira/universe#5088

The situation got better. I can start the application with blurhash 2.0.3; however, it's still failing in Jest tests. ๐Ÿ˜ž

Hi @Thisen! ๐Ÿ‘‹ There is still some breaking change in the patch release, as you can see in this PR: adeira/universe#5088

The situation got better. I can start the application with blurhash 2.0.3; however, it's still failing in Jest tests. ๐Ÿ˜ž

I can't speak into that. I'd need a new issue with a reproduction :)

janpe commented

Hi @Thisen! ๐Ÿ‘‹ There is still some breaking change in the patch release, as you can see in this PR: adeira/universe#5088

The situation got better. I can start the application with blurhash 2.0.3; however, it's still failing in Jest tests. ๐Ÿ˜ž

I'm seeing this as well. Jest tests break at 2.0.3. @mrtnzlml did you find a workaround?

janpe commented

Ok I see that adding blurhash into transformIgnorePatterns in Jest config is a workaround for this bug. Not sure where this happens but it seems like a bug in this project @Thisen

Ok I see that adding blurhash into transformIgnorePatterns in Jest config is a workaround for this bug. Not sure where this happens but it seems like a bug in this project @Thisen

A new issue with a reproduction would be great. ๐Ÿ‘๐Ÿผ

@janpe I didn't have time to check it yet or prepare a repro. Thanks for the suggested workaround! ๐Ÿ‘ It is certainly a breaking change in a patch release. ๐Ÿ˜ž