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

Always resolves to CJS even when using ESM #45

Open
jcbhmr opened this issue Jun 7, 2023 · 0 comments
Open

Always resolves to CJS even when using ESM #45

jcbhmr opened this issue Jun 7, 2023 · 0 comments

Comments

@jcbhmr
Copy link

jcbhmr commented Jun 7, 2023

When using the usual ESM stuff, I still resolve to index.js instead of index.esm.js.

import { createRequire } from "node:module";
const require = createRequire(import.meta.url);

console.table({
  "is-what": {
    esm: import.meta.resolve("is-what"),
    cjs: require.resolve("is-what"),
  },
  tosource: {
    esm: import.meta.resolve("tosource"),
    cjs: require.resolve("tosource"),
  },
});

(Using is-what as an example of it done OK-ly)

image

I think this could be solved by using modern exports: {} conditions instead of main and module? But even this is not recommended so as to avoid the dual package hazard https://nodejs.org/api/packages.html#packages_dual_package_hazard where you can get two copies of the package code in your webpack/vite/rollup/parcel/whatever bundle.js file.

The idealized solution is to either a) only export an ESM version or b) export an ESM wrapper that just export {} from "./module.cjs" so that there's only one canonical source of code or c) only export a CJS version.

Funnily enough, in the package's current state it doesn't suffer from the dual package hazard since it currently only exports CommonJS 🤣

@jcbhmr jcbhmr changed the title Node.js doesn't pick up ESM export Always resolves to CJS, not ESM export Jun 7, 2023
@jcbhmr jcbhmr changed the title Always resolves to CJS, not ESM export Always resolves to CJS even when using ESM Jun 7, 2023
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

1 participant