Publish project as dual ES modules and CommonJS package
npm install dual-publishPublish JS project as dual ES modules and CommonJS package to npm.
* Tested on [projects] with 16M downloads per month.
* [CI tests] with it works with Node.js, browsers, React Native,
bundlers (webpack, Parcel, Rollup, or esbuild) and CDNs (like jspm).
* Does not change line numbers in stacktrace and keep sources readable.
* No build step. No need for separated src/ and dist/ dirs in repository.
You will be able to test branch by installing version from GitHub like
npm i example@you/example#fix.
* Multiple files support. Your user will be able to import separated files
like import { nanoid } from 'nanoid/async'.
* Cleans npm package from development configs [before publishing].
* Supports process.env.NODE_ENV for development checks, which you want
to remove in production JS bundle.
You write CommonJS in your npm library sources:
``js`
// index.js
module.exports = { lib }
npx dual-publish compiles your library during publishing to npm:
`js
// index.js
export { lib }
// index.cjs
module.exports = { lib }
// package.json
{
…
"type": "module",
"module": "index.js",
"main": "index.cjs",
"exports": {
"require": "./index.cjs",
"import": "./index.js"
}
}
`
Now your library can be imported natively as ESM or CommonJS:
`js
// CommonJS
let { lib } = require('lib')
// ESM in Node.js, webpack, Parcel, and Rollup
import { lib } from 'lib'
// ESM in browser
import { lib } from 'https://cdn.jsdelivr.net/npm/lib/index.js'
``
[before publishing]: https://github.com/shashkovdanil/clean-publish/
[CI tests]: https://github.com/ai/dual-publish/blob/master/test/index.test.js
[projects]: https://github.com/search?l=JSON&q=%22dual-publish%22&type=Code
alt="Sponsored by Evil Martians" width="236" height="54">