Skip to content

Always resolves to CJS even when using ESM #45

Description

@jcbhmr

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 🤣

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions