rollup-plugin-externals
raw JSON → 0.0.1 verified Mon Apr 27 auth: no javascript
A Rollup plugin that mimics webpack's externals configuration, allowing you to mark dependencies as external and provide global variable names for bundling with script tags. Currently at v0.0.1, it is a lightweight alternative to @rollup/plugin-node-resolve combined with manual external handling. It maps module IDs to global variable names, emitting `module.exports = window.VariableName` stubs at build time. Suitable for library authors bundling for browser environments. No dependencies and ships TypeScript types.
Common errors
error TypeError: (0 , rp.default) is not a function ↓
cause CommonJS require used without destructuring, getting the module object instead of the externals function.
fix
Use
const { externals } = require('rollup-plugin-externals') instead of const externals = require('rollup-plugin-externals'). error RollupError: Could not resolve './some-internal-module' from 'node_modules/rollup-plugin-externals/index.js' ↓
cause The plugin's internal import resolution fails when used as a transitive dependency or with hoisted node_modules.
fix
Ensure rollup-plugin-externals is installed as a direct devDependency, and delete node_modules/.cache if needed.
error Uncaught ReferenceError: module is not defined ↓
cause Using the plugin with output.format: 'es' or 'iife' but the plugin generates CJS-style stubs.
fix
Set Rollup's output.format to 'cjs' or 'iife' and ensure the target environment supports
module. Warnings
gotcha The plugin emits `module.exports = window.VariableName` for CJS output; may cause issues with Rollup's `output.format: 'es'` if not configured correctly. ↓
fix Use `output.format: 'iife'` or ensure your output format supports global variable assignments.
gotcha Only works with Rollup; not compatible with other bundlers like webpack or esbuild. ↓
fix For webpack, use built-in `externals` config; for esbuild, use `external` option with `globalName`.
deprecated Plugin is at v0.0.1; API may change in future versions without major version bump. ↓
fix Pin to exact version and test upgrades thoroughly.
Install
npm install rollup-plugin-externals yarn add rollup-plugin-externals pnpm add rollup-plugin-externals Imports
- externals wrong
import externals from 'rollup-plugin-externals'correctimport { externals } from 'rollup-plugin-externals' - externals (require) wrong
const externals = require('rollup-plugin-externals')correctconst { externals } = require('rollup-plugin-externals') - TypeScript type
import type { ExternalsOptions } from 'rollup-plugin-externals'
Quickstart
import { defineConfig } from 'rollup';
import { externals } from 'rollup-plugin-externals';
export default defineConfig({
input: 'src/index.js',
output: {
dir: 'dist',
format: 'iife',
},
plugins: [
externals({
'react': 'React',
'react-dom': 'ReactDOM',
}),
],
});