Output Formats: ESM, CJS, and UMD

Understand four Rollup output formats, when to use each, and how to generate multiple formats in one build.

🎯 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 import it in Vite / Webpack projects.
  • Others will require it 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

FormatFull nameTarget environmentTypical usage
esES ModuleModern browsers and bundlersimport { add } from 'my-utils'
cjsCommonJSTraditional Node.js environmentsconst { add } = require('my-utils')
umdUniversal Module DefinitionBrowsers and Node.jsLoad with a <script> tag
iifeImmediately Invoked Function ExpressionBrowsersLoad with a <script> tag and execute immediately

A simple rule of thumb:

  • For libraries used by others: provide at least es and cjs for bundlers and Node.js.
  • For direct browser use: provide umd or iife.
Both 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:

rollup.config.mjs
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.

The 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:

dist/bundle.esm.js
function add(a, b) {
    return a + b;
}

export { add };

CJS output converts export into the exports object understood by Node.js:

dist/bundle.cjs.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:

dist/bundle.umd.js
(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:

  1. Add es and cjs outputs to the output array, named bundle.esm.js and bundle.cjs.js.
  2. Run npm run build and compare how the two files express exports.
rollup.config.mjs
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.