You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sharp should export an .mjs file so that it can be used in environments that doesn't support .cjs
What alternatives have you considered?
None but any suggestion is welcomed
Reacted by Anthony, benjaminlewandowski, Julien Cayzac, patricknazar, stealthmonkey99, Ziad, johnreyev, Artur Klesun, Marco Fugaro, Waldemar Lehner and 19 more
@lovell While Node.js ESM can work with CJS libraries and bundlers like webpack/rollup allow mixed syntax (that's how nitro workers) it would be nice if Sharp also adds exports field. This can make support less hacky and more ESM native. If you want me can make a PR to improve packages' ESM support. Some of popular packages even considered being esm-only.
Thanks for confirming, a PR to add a thin ESM wrapper and suitable exports to allow dual-usage would be most welcome. If it's possible to add a unit test for the wrapper, that would be great too.
And thank you Pooya for all your work on Nuxt and the Vue ecosystem!
Hi @lovell. Just pinging to say still would love to contribute to this for ESM support! @danielroe is also an ESM expert who might help on this as well :)
Reacted by Ramzi Youssef, Robin Vey, Daniel Roe and Julien Cayzac
@pi0 Do you happen to have an example with sharp and nuxt nitro somewhere? Running into problems, where sharp is a dependency of a package that is imported for a server side api in nuxt 3.
@lovell I started looking into this. I think an important question to answer to start would be: should sharp ship as ESM-only? AFAIK pros and cons are:
Pros
Cons
ESM+CJS
Compatibility, no change needed to engines.node
Increase in size. Needs duplicated files or a build system (eg. tsdown)
ESM-only
Easy, just convert
Compatibility, needs engines.node as ^20.19.0 || >=22.12.0 to support require(esm) without a flag
Reacted by Guillaume Khayat, João Ferreira and Antoine Moreaux
@florian-lefebvre In my opinion, it’s a good idea to switch to ESM-only and, for legacy apps, maybe create a repository for a community-driven CommonJS-only fork.
@guillaumekh We’ve been running sharp in Lambda since 2016. The last update was in 2018, I think, and it’s still working. I am still happy with webpack for bundling and reducing size.
For those interested in ESM as an aid for code bundling, some of the dynamic requires have been replaced with static paths via commit fff7431, which will be part of the next release and is independent of any changes being discussed here.
It should work if the current build is going to continue to build to CommonJS. The magic around the importing is mostly just in the package.json that explains how certain environments should import the module between the "types" and "exports".
If you use "module" as the type in project.json (like the article suggets) just remember to name the CommonJS export .cjs instead of .js, since in an ESM environment .js files would be treated as ESM by default.
You could try this and retain the current .js files output:
If you're going to build to ESM first though then you might need something different as ESM environments seems to be able to deal with the internal require(...) calls, but CommonJS environments won't handle import statements.
For anyone trying to bundle Sharp in an Electron app while keeping an ESM build, here’s what I learned after spending quite some time debugging this.
I initially explored approaches like this one: github:image-optimizer-electron
It works, but feels more like a workaround than a solid solution.
Key observations
Sharp is CommonJS → It cannot be properly bundled into an ESM build and must be marked as external.
Native dependencies live in @img/* → These contain platform-specific binaries and must be unpacked from the asar.
Runtime resolution constraint → sharp expects its dependencies (@img/*, detect-libc, semver, etc.) to be located relative to itself inside node_modules.
asar implication → Because of this resolution logic, both sharp and its native deps must live in app.asar.unpacked, otherwise runtime resolution fails.
Practical solution
Keep sharp and its native dependencies in app.asar.unpacked
Redirect imports at build time instead of trying to bundle
Activity
lovell commentedon Nov 17, 2021
Do you know if the wrapper approach outlined in https://nodejs.org/api/packages.html#approach-1-use-an-es-module-wrapper will work with Nuxt Nitro?
Rigo-m commentedon Nov 18, 2021
It should work, I think. Otherwise, you can use https://github.com/unjs/mlly to port sharp into ESM 😛
@pi0 any suggestions?
pi0 commentedon Nov 18, 2021
@Rigo-m Sharp can be imported inside Node.js ESM files and also working fine with Nuxt Nitro (the only limitation is you cannot deploy to Workers because of Node.js bindings). BTW you might like to use ipx which is based on Sharp but easier to integrate. Check this example: https://codesandbox.io/s/quirky-currying-3259v?file=/server/api/ipx.ts
@lovell While Node.js ESM can work with CJS libraries and bundlers like webpack/rollup allow mixed syntax (that's how nitro workers) it would be nice if Sharp also adds exports field. This can make support less hacky and more ESM native. If you want me can make a PR to improve packages' ESM support. Some of popular packages even considered being esm-only.
lovell commentedon Nov 18, 2021
Thanks for confirming, a PR to add a thin ESM wrapper and suitable
exportsto allow dual-usage would be most welcome. If it's possible to add a unit test for the wrapper, that would be great too.And thank you Pooya for all your work on Nuxt and the Vue ecosystem!
pi0 commentedon Apr 19, 2022
Hi @lovell. Just pinging to say still would love to contribute to this for ESM support! @danielroe is also an ESM expert who might help on this as well :)
robinscholz commentedon May 16, 2022
@pi0 Do you happen to have an example with sharp and nuxt nitro somewhere? Running into problems, where sharp is a dependency of a package that is imported for a server side api in nuxt 3.
pi0 commentedon May 16, 2022
Usage is in sandbox comment above but official Nuxt3 integration is coming soon with Image module.
shtse8 commentedon Jul 13, 2022
looking forward to this support
spartanz51 commentedon Nov 23, 2022
TimisRobert commentedon Dec 3, 2022
I'm trying to make it work with esbuild, but I always get __dirname is not defined :(
vikyw89 commentedon May 11, 2023
maddsua commentedon Aug 1, 2023
florian-lefebvre commentedon Mar 17, 2026
@lovell I started looking into this. I think an important question to answer to start would be: should sharp ship as ESM-only? AFAIK pros and cons are:
engines.nodeengines.nodeas^20.19.0 || >=22.12.0to supportrequire(esm)without a flagguillaumekh commentedon Mar 17, 2026
IMHO it would be a fair ask.
Here are some interesting stats on the current CJS/ESM landscape if anyone is curious.
florian-lefebvre commentedon Mar 17, 2026
Apparently if Sharp was ESM-only, this dynamic import
sharp/lib/sharp.js
Line 82 in 7b4c476
So basically that leaves 2 solutions:
sharp = await loadSharp()Bessonov commentedon Mar 17, 2026
@florian-lefebvre In my opinion, it’s a good idea to switch to ESM-only and, for legacy apps, maybe create a repository for a community-driven CommonJS-only fork.
@guillaumekh We’ve been running sharp in Lambda since 2016. The last update was in 2018, I think, and it’s still working. I am still happy with webpack for bundling and reducing size.
lovell commentedon Mar 17, 2026
For those interested in ESM as an aid for code bundling, some of the dynamic requires have been replaced with static paths via commit fff7431, which will be part of the next release and is independent of any changes being discussed here.
In terms of supporting both ESM+CJS (until Node.js 22 reaches end of life), does anyone know if "approach 1" documented at https://github.com/nodejs/package-examples/tree/main/guide/07-dual-packages#approach-1-use-an-es-module-wrapper will work?
dereekb commentedon Mar 18, 2026
It should work if the current build is going to continue to build to CommonJS. The magic around the importing is mostly just in the
package.jsonthat explains how certain environments should import the module between the "types" and "exports".If you use "module" as the type in
project.json(like the article suggets) just remember to name the CommonJS export.cjsinstead of.js, since in an ESM environment.jsfiles would be treated as ESM by default.You could try this and retain the current
.jsfiles output:wrapper.mjs
package.json changes
If you're going to build to ESM first though then you might need something different as ESM environments seems to be able to deal with the internal
require(...)calls, but CommonJS environments won't handleimportstatements.florian-lefebvre commentedon Mar 18, 2026
AFAIK using an ES module wrapper is called Faux-ESM, which has no real benefit over just CJS
Yug-Damon commentedon Mar 29, 2026
Keywords: Sharp + Electron + ESM bundling
For anyone trying to bundle Sharp in an Electron app while keeping an ESM build, here’s what I learned after spending quite some time debugging this.
I initially explored approaches like this one:
github:image-optimizer-electron
It works, but feels more like a workaround than a solid solution.
Key observations
Practical solution
Example using an esbuild plugin:
And in your Electron config:
{ "build": { "asarUnpack": [ "node_modules/sharp/**/*", "node_modules/@img/**/*", "node_modules/detect-libc/**/*", "node_modules/semver/**/*" ] } }Conclusion
Even with ESM tooling (esbuild, Vite, etc.), Sharp behaves as a runtime-resolved native module, not a bundle-friendly dependency.
So the reliable approach today is:
If someone turns this into a reusable plugin/package, that would definitely help the ecosystem 👍
To address more fundamentally the issue I would suggest :
lovell commentedon Jun 10, 2026
sharp v0.35.0 now available with dual ESM/CJS support. Merci beaucoup to @florian-lefebvre for helping with this.