-
-
Notifications
You must be signed in to change notification settings - Fork 905
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
[BUG] CommonJS file with ESM syntax is exported as "module" #692
Comments
Marking as stale due to 90 days with no activity. |
Marking as stale due to 90 days with no activity. |
Closing issue due to 30 days since being marked as stale. |
We're running into this issue with Jest tests in a The fix suggested by @cdauth sounds like it should be relatively low impact (compared to adding |
This would be helpful for us too. Alternatively switch the |
Before you begin...
Description of the problem
The exports in
package.json
currently contains this:In a Node.js environment, the
.js
files are interpreted as CommonJS and the.mjs
file as ESM, since no"type": "module"
is defined. This means that the files in./dist/esm-*/*.js
are interpreted as CJS, even though they contain ESM code.In a normal Node.js ESM environment, this is not a problem, since it will resolve to the
.mjs
file defined in thenode.import
export. However, there are other environments which resolve dependencies in a different way but still interpret the file extensions in the same way as Node.js.Problem with esbuild
In particular, I'm having trouble with a project that is compiled using
esbuild
. According to its docs, innode
mode, themodules
condition is included by default, so thenode.module
export is used, leading to the following error:As a workaround, I specified
conditions: []
in my esbuild config.Problem with jest
I'm also experiencing the same problem with Jest that is reported in #678. Testing has revealed that Jest (at least in a jsdom environment) uses the
browser.import
export.Suggested solution
In other issues reported here I have seen comments about needing to add
"type": "module"
, which would have a lot of side effects and break things. I think this is not needed, it is only needed to rename the ESM bundles to.mjs
. I believe this would also make thewrapper.mjs
file unnecessary.There might be some outdated build tools that don't support the
.mjs
extension yet. In immerjs/immer#939, we had this problem with Metro, however the underlying issue facebook/metro#535 seems to have been fixed by now. If support for such outdated build tools should be preserved, a.js
ESM file should be referenced in themodule
field inpackage.json
(while a.mjs
file should be in theexports.module
field).Recipe for reproducing
To reproduce the issue with
esbuild
, create a filetest.mjs
that importsuuid
:Then build the file by executing a Node.js script with the following content:
It is important to use a recent version of esbuild, as older versions were not using the
module
condition.Additional information
No response
Environment
No response
The text was updated successfully, but these errors were encountered: