@guybedford, @jkrems and I discussed the package dual-ESM/CommonJS case and we have a small proposal, based on the current ecmascript-modules implementation:
-
The package.json "main" field reverts to its prior CommonJS-only use.
-
A new field "exports" is created that takes a string like "./src/index.js". This is the ES module entry point. "exports" is to import what "main" is to require.
Notes:
-
"exports" may in the future take an object, preserving design space for the package exports proposal.
-
If "exports" points to a .js file and "type": "module" is not set, an error is thrown similar to the “type mismatch” errors (like using --type=commonjs with an .mjs file). The error would also instruct the user to add "type": "module" to package.json. The "exports" field does not imply "type": "module".
And that’s it! This should cover the case while preserving design space for future proposals, and for Node potentially switching to ESM by default someday.