[Pkg-javascript-devel] Export both ESM and CommonJS modules
Yadd
yadd at debian.org
Sun Sep 12 17:29:07 BST 2021
Hi all,
some recent updates switched from CommonJS to ESM ("type: module"). I
found a way for node-strip-json-comments which can be applied to most of
Sindre Sorhus modules:
1. build a commonjs file:
babeljs -o index.cjs index.js
perl -i -pe 's/^export default/module.exports =/' index.cjs
2. adapt package.json:
--- a/package.json
+++ b/package.json
@@ -11,7 +11,12 @@
"url": "https://sindresorhus.com"
},
"type": "module",
- "exports": "./index.js",
+ "exports": {
+ "import": "./index.js",
+ "require": "./index.cjs"
+ },
+ "main": "./index.cjs",
+ "module": "./index.js",
"engines": {
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
@@ -20,6 +25,7 @@
"bench": "matcha benchmark.js"
},
"files": [
+ "index.cjs",
"index.js",
"index.d.ts"
],
For more complex packages, see
https://www.sensedeep.com/blog/posts/2021/how-to-create-single-source-npm-module.html
Cheers,
Yadd
More information about the Pkg-javascript-devel
mailing list