CommonJS
Node.js only, and never part of the language.
function add(a, b) {
return a + b;
}
module.exports = { add };const { add } = require('./math.cjs');
console.log(add(1, 2));esmodule.com
Introduced in the ES6 specification, the ECMAScript modules (ES modules or ESM) format is the standardized format for packaging and organizing JavaScript code. It provides a native way to import and export code across files and packages.
ES modules enable better code organization, dependency management, and enable tools to perform static analysis for optimizations like tree-shaking. Since its official addition to the JavaScript specification, ES modules are now widely adaopted in modern browsers and other runtime environments, making it the universal standard for modularizing JavScript code.
Node.js only, and never part of the language.
function add(a, b) {
return a + b;
}
module.exports = { add };const { add } = require('./math.cjs');
console.log(add(1, 2));The same code, in every runtime and bundler.
export function add(a, b) {
return a + b;
}import { add } from './math.js';
console.log(add(1, 2));If you want to migrate your project to ES modules, check out the guides for one which covers your stack!
Update from CommonJS to ES Module output in esbuild
Switch a Rolldown build to ESM output and drop the CommonJS bundle.
Switch a Rollup build to ESM output and drop the CommonJS bundle.
Guides are markdown files in guides/. Add one and open a pull request.
1,378,002+ npm packages publish ES modules
Whether the frameworks you already use publish ES modules on npm.
next CJS only Next.js is CommonJS only.nuxt ESM only Nuxt publishes ES modules.astro ESM only Astro publishes ES modules.@sveltejs/kit ESM only SvelteKit publishes ES modules.react-router ESM only React Router publishes ES modules.@solidjs/start ESM only SolidStart publishes ES modules.