rollup-plugin-rewrite-imports
raw JSON → 2.0.0 verified Mon Apr 27 auth: no javascript maintenance
Rollup plugin that rewrites ES module import and dynamic import paths by prepending a string to each path. Version 2.0.0 is the latest stable release; maintained on an as-needed basis. It solves the problem of fragmented builds where dependencies reside in separate node_modules directories, enabling theme developers and platform owners to use standard web component tooling while keeping builds isolated. Differentiates from other path-rewriting plugins by focusing specifically on ESM imports in the context of legacy or multi-repo setups, with no external dependencies.
Common errors
error require() of ES Module /path/to/rollup-plugin-rewrite-imports not supported. ↓
cause Attempting to require() an ESM-only module with CommonJS require.
fix
Use dynamic import() or switch to import syntax, or add .default: const rewriteImports = require('rollup-plugin-rewrite-imports').default;
error Cannot find module 'rollup-plugin-rewrite-imports' ↓
cause Package not installed or not in node_modules.
fix
Run
npm install rollup-plugin-rewrite-imports --save-dev. error import { rewriteImports } from 'rollup-plugin-rewrite-imports' returns undefined. ↓
cause Named import used but the module exports a default export, not a named one.
fix
Use default import: import rewriteImports from 'rollup-plugin-rewrite-imports';
Warnings
breaking Version 2.0.0 changed to ESM-only. CommonJS require() will fail without .default access. ↓
fix Use import syntax or require with .default, or downgrade to 1.x if CommonJS is required.
deprecated The plugin does not support CJS; Node.js <12 or older bundlers may not handle ESM imports correctly. ↓
fix Ensure your runtime supports ESM or bundle with a tool that handles ESM-to-CJS conversion.
gotcha The plugin rewrites imports based on a simple string prefix; it does not understand module resolution. All imports (relative and bare) are prefixed indiscriminately. ↓
fix Use with care: ensure the prefix does not break valid import paths. Pair with rollup-plugin-auto-external or a resolve plugin to manage external dependencies.
Install
npm install rollup-plugin-rewrite-imports yarn add rollup-plugin-rewrite-imports pnpm add rollup-plugin-rewrite-imports Imports
- rewriteImports wrong
const rewriteImports = require('rollup-plugin-rewrite-imports')correctimport rewriteImports from 'rollup-plugin-rewrite-imports' - rewriteImports wrong
const rewriteImports = require('rollup-plugin-rewrite-imports')correctconst rewriteImports = require('rollup-plugin-rewrite-imports').default - rewriteImports wrong
import { rewriteImports } from 'rollup-plugin-rewrite-imports'correctimport rewriteImports from 'rollup-plugin-rewrite-imports'
Quickstart
import rewriteImports from 'rollup-plugin-rewrite-imports';
import autoExternal from 'rollup-plugin-auto-external';
export default {
input: 'src/custom.js',
output: {
file: 'build/custom.esm.js',
format: 'esm',
},
plugins: [
autoExternal(),
rewriteImports('../../build/es6/node_modules/'),
],
};