dualmodule

A way to test dual modules in Node.js

Usage no npm install needed!

<script type="module">
  import dualmodule from 'https://cdn.skypack.dev/dualmodule';
</script>

README

Node.js Dual Modules

The minimal amount of effort needed to publish modules, for both ESM and CJS, without ever needing to write an .mjs, or .cjs file.

Test It !

You need node 13.10 or higher, and a test folder, such as ~/test/node-modules to perform this test:

mkdir -p ~/test/node-modules
cd ~/test/node-modules
npm i dualmodule
# test CommonJS
node -e 'console.log(require("dualmodule"))'; # read "CJS"
# test ESM
node --input-type=module -e 'import $ from "dualmodule"; console.log($)';

If you are in node < 13.10 you'll see an ExperimentalWarning, but the output is ESM.

If you are in node >= 13.10 you won't see the warning, just the ESM output.

If you'd like to test through any file.js in that folder, and you want to test the ESM version of the module:

echo '{"type":"module"}' > ~/test/node-modules/package.json
echo 'import $ from "dualmodule"; console.log($)' > test.js
node test.js # read "ESM"

And you are good to go 🎉