Common Rollup Plugins

Learn to use node-resolve, commonjs, json, and terser, and understand plugin order.

🎯 Introduction

By the end of this article, you will know how to use four common official Rollup plugins to resolve third-party dependencies, convert CommonJS modules, import JSON, and minify bundles.


🧱 Why Do We Need Plugins?

Rollup's core is deliberately focused: it understands the ES modules in your project.

In practice, you will often encounter these situations:

  • import { debounce } from 'lodash-es' references a dependency in node_modules.
  • An older package uses CommonJS and exposes only module.exports.
  • You want to read a version number with import info from './package.json'.
  • You want to minify your code into a single line before publishing.

Plugins handle these requirements. The Rollup team maintains a collection of plugins with names beginning with @rollup/plugin-.

This course uses Node.js v22.0.0 and the following plugin versions:

npm install @rollup/plugin-node-resolve@16.0.3 @rollup/plugin-commonjs@29.0.3 @rollup/plugin-json@6.1.0 @rollup/plugin-terser@1.0.0 --save-dev

Let's look at each plugin in turn.


🧰 node-resolve: Resolving Third-Party Dependencies

Why might Rollup fail to find lodash-es even after you run npm install lodash-es?

The key is that npm install downloads files into node_modules, but Rollup's core does not implement the rules for looking up packages there. These two kinds of imports follow different resolution rules:

// ① Relative path: Rollup can resolve this directly from the current file
import { add } from './math.js';

// ② Bare specifier: a package name without a path, which Rollup's core cannot resolve
import { debounce } from 'lodash-es';

The second form needs a dedicated resolution algorithm to locate the actual file:

  1. Find the lodash-es directory in node_modules.
  2. Read its package.json and use fields such as main, module, and exports to determine the entry point—which of the package's hundreds of files should be loaded?
  3. Resolve the specific file, such as node_modules/lodash-es/lodash.js.

These rules are known as Node.js module resolution. Node.js includes them, but Rollup deliberately leaves them out of its core to keep it small and because its original focus on libraries assumes dependencies can be provided externally. The node-resolve plugin adds this resolution capability to Rollup:

rollup.config.mjs
import resolve from '@rollup/plugin-node-resolve';

export default {
    input: 'src/index.js',
    output: {
        file: 'dist/bundle.js',
        format: 'es'
    },
    plugins: [resolve()]
};

With the plugin enabled, lodash-es is included in the bundle.

Without the plugin, Rollup prints (!) Unresolved dependencies rather than immediately failing. It leaves 'lodash-es' in the output as an external dependency. Running that output directly in a browser fails because browsers do not resolve bare specifiers either. Remember to add resolve() when bundling third-party packages.
You may never have installed this kind of plugin in Webpack / Vite because they include node_modules resolution. Rollup provides it as a plugin that you add explicitly.

🧰 commonjs: Converting CommonJS Modules

Many established npm packages use CommonJS (module.exports = ...). Rollup understands ES modules, so using these packages without conversion can cause errors.

The commonjs plugin converts CommonJS modules into ES modules:

rollup.config.mjs
import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';

export default {
    input: 'src/index.js',
    output: {
        file: 'dist/bundle.js',
        format: 'es'
    },
    plugins: [resolve(), commonjs()]
};
Place commonjsafterresolve(). Rollup processes plugins in array order: it needs to find the file before converting it. Reversing this order is a common beginner mistake.

🧰 json: Importing JSON Files

Use the json plugin to read JSON files directly in your code, such as the version in package.json:

rollup.config.mjs
import json from '@rollup/plugin-json';

export default {
    input: 'src/index.js',
    output: {
        file: 'dist/bundle.js',
        format: 'es'
    },
    plugins: [json()]
};

You can then import JSON just like a module:

src/index.js
import pkg from '../package.json';

export function getVersion() {
    return pkg.version;
}

During bundling, the JSON becomes a regular JavaScript object embedded in the output.

The json plugin includes only the fields you use. If package.json has dozens of fields but you only use version, the output includes only version; tree shaking removes the other fields.

🧰 terser: Minifying Code

Before publishing, you typically remove whitespace and comments and shorten variable names. The terser plugin handles this. Here, place it in output.plugins rather than the top-level plugins array:

rollup.config.mjs
import terser from '@rollup/plugin-terser';

export default {
    input: 'src/index.js',
    output: [
        {
            file: 'dist/bundle.js',
            format: 'es'
        },
        {
            file: 'dist/bundle.min.js',
            format: 'es',
            plugins: [terser()]
        }
    ]
};

This configuration produces two bundles: readable code for debugging and minified code for distribution.

The difference is easy to see:

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

export { add };
dist/bundle.min.js
function n(n,t){return n+t}export{n as add};
Why use output.plugins for terser? Minification happens after bundle generation, during the output phase. It also works in the top-level plugins array, but placing it in an output configuration lets you minify only that particular bundle.

❓ Questions and Answers

Q1: Why do I get Unresolved dependencies after running npm install?

Installation only places files in node_modules. Rollup's core does not resolve bare specifiers; the node-resolve plugin supplies that capability. Add resolve() to plugins to address the warning.

Q2: Does every third-party package need the commonjs plugin?

No. Many packages now provide ES modules, such as lodash-es, and only need node-resolve. The commonjs plugin is needed for packages that provide only CommonJS.

Q3: Do plugins always execute from left to right?

Yes. The plugins array determines the order. Remember: resolve first, convert with commonjs next, then apply other plugins.

Q4: Is minified code easy to debug when something goes wrong?

Debugging it directly is difficult because variable names are shortened. This is where the sourcemap option from the previous article helps: the .map file maps the output back to the source.

Q5: Are there Rollup plugins for CSS and images?

Yes. Available plugins include rollup-plugin-postcss and @rollup/plugin-image. You will rarely need them for JavaScript utility libraries, but it helps to know they exist.


🧪 Exercise

Add plugin support to rollup-demo:

  1. Install the four plugins covered here using the versions in the installation command at the beginning of this article.
  2. Import ../package.json in src/index.js and export a getName function that returns the package name.
  3. Configure rollup.config.mjs to produce a regular bundle and a minified .min.js bundle.
rollup.config.mjs
import json from '@rollup/plugin-json';
import terser from '@rollup/plugin-terser';

export default {
    input: 'src/index.js',
    output: [
        {
            file: 'dist/bundle.js',
            format: 'es'
        }
        // Add the minified bundle.min.js output here
    ],
    plugins: [json()]
};

🎉 You now know Rollup's common plugins! Next, we will explore one of its signature features: tree shaking.