Output Formats: ESM, CJS, and UMD
🎯 Introduction
By the end of this article, you will understand the differences between es, cjs, umd, and iife, know which environments they suit, and be able to generate multiple outputs in one build.
🧱 Why Do We Need Output Formats?
People may use your library in different environments:
- Some will
importit in Vite / Webpack projects. - Others will
requireit in older Node.js projects. - Others will load it directly in a browser with a
<script>tag.
These environments use different module systems. Rollup's output.format option converts the same source code into different module formats.
⚡ Comparing Four Common Formats
| Format | Full name | Target environment | Typical usage |
|---|---|---|---|
es | ES Module | Modern browsers and bundlers | import { add } from 'my-utils' |
cjs | CommonJS | Traditional Node.js environments | const { add } = require('my-utils') |
umd | Universal Module Definition | Browsers and Node.js | Load with a <script> tag |
iife | Immediately Invoked Function Expression | Browsers | Load with a <script> tag and execute immediately |
A simple rule of thumb:
- For libraries used by others: provide at least
esandcjsfor bundlers and Node.js. - For direct browser use: provide
umdoriife.
umd and iife work in browsers. The difference is that umd also supports Node.js and AMD environments, whereas iife targets browsers. If you are unsure, choose umd.🛠 Generating Multiple Formats
Set output to an array to generate multiple bundles in one build:
export default {
input: 'src/index.js',
output: [
{
file: 'dist/bundle.esm.js',
format: 'es'
},
{
file: 'dist/bundle.cjs.js',
format: 'cjs'
},
{
file: 'dist/bundle.umd.js',
format: 'umd',
name: 'MyUtils'
}
]
};
Running npm run build creates three files in dist, each targeting a different environment.
umd and iife formats require a name for the global variable exposed in the browser. With name: 'MyUtils' above, you can call window.MyUtils.add(1, 2). Omitting it causes an error.💡 What Do the Three Outputs Look Like?
The same source code produces noticeably different output in each format. Compare the examples below.
ES output closely resembles the combined source, retaining export:
function add(a, b) {
return a + b;
}
export { add };
CJS output converts export into the exports object understood by Node.js:
'use strict';
function add(a, b) {
return a + b;
}
exports.add = add;
UMD output adds a wrapper that detects the environment and selects the appropriate export mechanism:
(function (global, factory) {
// Detect Node.js, AMD, or a browser and choose the appropriate export mechanism
typeof exports === 'object' && typeof module !== 'undefined'
? factory(exports)
: (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.MyUtils = {})));
})(this, function (exports) {
'use strict';
// ...
});
UMD may look complex, but Rollup generates it for you; you do not need to write it yourself.
❓ Questions and Answers
Q1: Can I provide only es if my library is used exclusively in new projects?
Yes. If all consumers use Vite / Webpack, providing only es is the simplest option. Open-source libraries usually also provide cjs for compatibility.
Q2: Is there a difference between format: 'es' and format: 'esm'?
No. esm is an alias for es, so either works. Similarly, commonjs is an alias for cjs.
Q3: Can a UMD name contain a hyphen, such as my-utils?
Avoid it. Since name becomes a global variable name, use a valid JavaScript identifier. PascalCase names such as MyUtils are recommended.
Q4: Does generating multiple formats slow down the build?
Slightly, because Rollup must write multiple files. Dependency analysis only runs once, so the practical impact is small.
🧪 Exercise
Update rollup-demo to generate two formats in one build:
- Add
esandcjsoutputs to theoutputarray, namedbundle.esm.jsandbundle.cjs.js. - Run
npm run buildand compare how the two files express exports.
export default {
input: 'src/index.js',
output: [
{
file: 'dist/bundle.esm.js',
format: 'es'
}
// Add the cjs output here
]
};
🎉 You now understand Rollup's output formats! Next, we will use plugins to handle JSON files and third-party dependencies.
