cyrilwanner / next-optimized-images

🌅 next-optimized-images automatically optimizes images used in next.js projects (jpeg, png, svg, webp and gif).
MIT License
2.21k stars 93 forks source link

Detection of optimization libraries incorrect with yarn berry + workspaces #227

Open osdiab opened 3 years ago

osdiab commented 3 years ago

Thanks for the great project!

With v2, I was using it in one nextJS project but then I made a new one where my NextJS project is part of a yarn workspaces monorepo, but when I start it up I get the error that image compression libraries aren't present.

so I moved the imagemin package deps to the root package.json of the yarn workspace, and now next-optimized-images is happy. It seems that next-optimized-images isn't looking at the actually installed packages for the monorepo subpackage, but I haven't looked into the code to see why that is.

osdiab commented 3 years ago

Seems that require.resolve() isn't a reliable way to determine if a package is installed for pnp packages, and that this library uses it for that purpose.

So the trick is probably to use a pnp-compatible package resolver like one referenced in the yarn article above.

osdiab commented 3 years ago

I don't know what are the pros and cons of libraries out there but Webpack has one. https://github.com/webpack/enhanced-resolve

osdiab commented 3 years ago

Perhaps the real move is to just use version 3 though.

merceyz commented 3 years ago

Seems that require.resolve() isn't a reliable way to determine if a package is installed for pnp packages, and that this library uses it for that purpose.

It is perfectly fine to use it for that, but only if you require from a location that declares them, i'm guessing the resolvePath is pointing to the wrong place

simPod commented 1 year ago

@osdiab how did you solve it? I tried to require imagemin but still getting error.

osdiab commented 1 year ago

Lol I just gave up on using it with pnp