ESM and CJS Dual Package Output
Generate both ES module and CommonJS builds with correct package.json exports field configuration.
What is ESM?
ESM (ECMAScript Modules) is the official JavaScript module standard using import and export syntax. ESM is statically analyzable — bundlers can determine at build time exactly which exports are used, enabling tree-shaking. Modern browsers and Node.js both support ESM natively.
What is CJS?
CJS (CommonJS) uses require() and module.exports syntax. It was Node.js's original module system and is still needed for compatibility with older Node.js environments, Jest (which historically used CJS), and code that uses require(). CJS is dynamically evaluated, making tree-shaking harder.
All lessons in this course
- Bundling with Rollup and tsup for Libraries
- ESM and CJS Dual Package Output
- Peer Dependencies and Tree Shaking
- Publishing to npm and Semantic Versioning