Missing `default` or `module-sync` export in package.json
SimpleCreations opened this issue · 8 comments
Description
Latest Node.js now supports require
ing pure ESM packages, which is great for all of us who are stuck with CJS due to other dependencies and want to use 7.x.
However, graphql-request
's exports
field in package.json is too restrictive — it insists on the package being imported via ESM syntax, while in reality it otherwise would work if imported via require(esm)
.
Lines 9 to 15 in ccdae10
Please consider adding an additional default
or module-sync
export, or removing import
specifier altogether, e.g.
"exports": {
".": {
"types": "./build/entrypoints/main.d.ts",
"default": "./build/entrypoints/main.js"
}
}
This is done by other pure ESM packages (for example nanoid
).
Reproduction Steps/Repo Link
In Node 23:
require("graphql-request");
node:internal/modules/cjs/loader:644
throw e;
^
Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main defined
@SimpleCreations Awesome, I also recently heard about this but just vaguely. I'll make a release with the changes you're indicating.
Do you know which Node.js version users need for this to work?
Hi @jasonkuhrt, thank you for quick reply!
require(esm)
was added in Node 20.17 and Node 22.0 with --experimental-require-module
flag, and also in Node 23.0 without flags. module-sync
in package.json is supported since Node 22.10.
The change I'm proposing would be backwards compatible of course.
What is the benefit of module-sync?
When we ship this I'll update our docs for users who don't know
Note you're asking about graphql-request
. I will make the change to graffle
. Do you need this change in graphql-request
too?
I also updated graphql-request
: https://github.com/graffle-js/graffle/releases/tag/7.1.1
Hi @jasonkuhrt! Thank you for the changes — I've tested the update, it works great now! Also glad you removed unnecessary dependencies.
Yes, I've been looking to upgrade graphql-request
from v6 to v7 for a while now but the CJS/ESM interop was the roadblock.
Thanks for brining this approach to my attention!