Skip to content
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

Export using .cjs / .mjs extensions #12

Closed
thislooksfun opened this issue Dec 22, 2022 · 2 comments
Closed

Export using .cjs / .mjs extensions #12

thislooksfun opened this issue Dec 22, 2022 · 2 comments

Comments

@thislooksfun
Copy link

Currently all the output files are using the .js extension. This is an issue because if the package.json file has "type": "module", then all the cjs/*.js imports error with the error:

require() of ES Module <file>.js from <file>.cjs not supported.
index.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in <path>/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

And, I suspect that the reverse is true as well (trying to import from a package without "type": "module"), though I haven't verified it.

In order to properly support these imports could you please either:

  • switch based on the package.json's "type":
    • with "type": "module", CommonJS files end in .cjs and ESM files end in .js
    • without "type": "module", CommonJS files end in .js and ESM files end in .mjs
  • or just always have all CommonJS files end in .cjs and all ESM files end in .mjs

I believe this is the cause of solidjs-community/solid-aria#75.

@sachaw
Copy link

sachaw commented May 11, 2023

The better way to handle this is to use the exports and main fields in package.json instead of using more file extensions that may not be supported in the future.

@amoutonbrady
Copy link
Collaborator

amoutonbrady commented Feb 10, 2025

Fixed in commit #da278453a09648f716e24828ea3b64cd0fe9eabe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants