-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
[RTK Alpha]: Invalid exports config for CJS consumption w/ Node #3208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
sigh this is exactly why I keep begging somebody, anybody, to put together a comprehensive and authoritative guide to doing this :( |
Yeah, sorry. Happy to help where I can, I've (unfortunately, I guess) dealt with this (and the following headaches) quite a bit while maintaining a few build tools. It's admittedly a lot of esoteric knowledge and a lot of "it depends on what you want to support". On the other hand, if no one has raised an issue yet, and you have enough users on the alpha, maybe no one's using RTK in Node/compliant environments and trying to consume CJS? Might be a non-issue, but one I thought I'd raise at least. |
I assume no one is using the alpha, period. Both because that's what always happens with pre-releases, and also because no one is raising feedback so far. I do honestly appreciate you filing the issue, but I am also legitimately getting angry at how messed up this whole situation is :( I want to do right by my users and support the variety of build tools and environments I expect they'll be using for their apps. But I can't do that if every single article and person is giving me contradictory instructions on what I'm supposed to do :( |
I'll try to write up a PR later tonight if I can for what my suggestion would be, but it really is a bad situation in the ecosystem in all reality. I think it's fair to say ecosystem collectively screwed up the CJS -> ESM transition pretty hard, from bundlers jumping on ESM early ( It's contradictory because the ecosystem is sadly contradictory. |
The following configuration should work across all tools:
This means you have two separate ESM builds, one with I saw this format from {
"main": "./dist/cjs/index.js",
"module": "./dist/es/index.js",
"exports": {
"./package.json": "./package.json",
".": {
"types": "./types/index.d.ts",
"import": "./dist/es/index.mjs",
"module": "./dist/es/index.js",
"default": "./dist/cjs/index.js"
}
}
} |
There's not much of a reason to add |
I think we can call this fixed as of the latest 2.0 alphas. |
I skimmed the issues but couldn't find anything matching, so apologies if it's a duplicate and/or intentional.
The current config in the Alpha does not allow for consumption of the CJS bundle in modern version of Node/any tool that follows Node's module resolution spec, as it uses
.js
to refer to a CJS module despite the package setting"type": "module"
. If"type": "module"
is set,.cjs
is necessary in"exports"
.Some bundlers do work around this and are more forgiving (whether or not that's a good thing is something else entirely), but this config will not work in Node and/or any environments that follow its resolution mechanism.
To reproduce:
Attempting to execute this file (
node index.cjs
) will result in the following expected error:Generally, it should be pretty safe to use
.cjs
in"exports"
. Both are relatively new, and any environment that supports consuming from"exports"
should support.cjs
. This may mean you'll want to duplicate your CJS bundles, however (provide as.js
for"main"
and the spec-adherent.cjs
for"exports"
), assuming you're not opposed to larger install size.The text was updated successfully, but these errors were encountered: