70 %
Chris Biscardi

cheap node 14 native esm support with wrapper.mjs

So you've got a package that already works in the ecosystem and node v14 with its native ESM support is about to go LTS and you've got users asking you to support node v14+ native ESM.

Here's the cheap way: wrap it.

In package.json, use conditional exports to point at a wrapper.mjs for import and your usual main export for require.

package.json
JS
{
"exports": {
"import": "./wrapper.mjs",
"require": "./dist/index.js"
}
}

Then in the new wrapper.mjs take advantage of node's commonjs/esm interop to re-export the exports. Note that you will have to import the default export for commonjs compatibility reasons.

wrapper.mjs
JS
import Pkg from './dist/index.js';
export const thing = Pkg.thing;
export const another = Pkg.another;