rollup-plugin-replace-imports

raw JSON →
1.0.0 verified Mon Apr 27 auth: no javascript

A Rollup plugin that replaces import paths in bundled output, useful for creating dual CJS/ESM builds by transforming module specifiers (e.g., replacing '/es/' with '/'). Version 1.0.0 is stable but sparsely documented; no known release cadence or major updates. Unlike more complex plugin chains, this focuses on a single transformation function applied after module resolution.

error Error: Cannot find module 'rollup-plugin-replace-imports'
cause Package not installed or dependency missing.
fix
npm install rollup-plugin-replace-imports --save-dev
error TypeError: replaceImports is not a function
cause Using CommonJS require instead of ESM import.
fix
Change to 'import replaceImports from 'rollup-plugin-replace-imports'' in an ESM context.
error The plugin 'replaceImports' must be placed in the output plugins array.
cause Incorrectly placed in top-level plugins.
fix
Move replaceImports inside output.plugins array.
gotcha Replace function is called after bundle generation; modifications to paths that do not exist will not error.
fix Ensure replacement string exists in at least one import path, or use a callback that logs unmatched paths for debugging.
breaking Plugin only works in output plugins array, not in top-level plugins array.
fix Place replaceImports inside the output's plugins array, not the top-level plugins.
gotcha TypeScript definitions are not published with the package; users must supply their own types.
fix Install @types/rollup-plugin-replace-imports separately (if available) or declare module manually.
npm install rollup-plugin-replace-imports
yarn add rollup-plugin-replace-imports
pnpm add rollup-plugin-replace-imports

Shows basic usage of replaceImports in a Rollup config to transform import paths from '/es/' to '/' for ESM output.

import replaceImports from 'rollup-plugin-replace-imports';
import resolve from '@rollup/plugin-node-resolve';

export default {
  input: 'src/index.js',
  output: {
    dir: 'dist',
    format: 'es',
    plugins: [
      replaceImports(n => n.replace('/es/', '/')),
    ],
  },
  plugins: [resolve()],
};