node-tosource icon indicating copy to clipboard operation
node-tosource copied to clipboard

Always resolves to CJS even when using ESM

Open jcbhmr opened this issue 2 years ago • 0 comments

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 avatar Jun 07 '23 05:06 jcbhmr

+1

qharlie avatar Dec 27 '11 18:12 qharlie

Is there any news on this

ualtinok avatar Jan 31 '12 12:01 ualtinok

I don't have any timeline for this (I haven't done any work on it). Definitely open to discussion/contribution.

marcello3d avatar Jan 31 '12 13:01 marcello3d

Many of the drivers seem to have a setReadPreference method on the server connection, collection, and cursor objects. Is that what this is? Because setSlaveOk may not be sufficient for some use cases.

awwright avatar Dec 01 '12 13:12 awwright