Registry / devops / rollup-plugin-commonjs-alternate

rollup-plugin-commonjs-alternate

JSON →
library0.8.0jsnpmunverified

A Rollup plugin that converts CommonJS modules to ES modules, designed as an alternative to @rollup/plugin-commonjs. Version 0.8.0 is the latest. It improves on the official plugin by supporting conditional requires (only including the correct branch), detecting require calls in ESM files (e.g., for React Hot Loader), and not stubbing dynamic requires (essential for HMR). It is useful for front-end libraries with best-practice module patterns, but may not work with libraries that reassign module.exports. Named exports require explicit configuration. Suitable for Rollup bundling in development and production.

npm install rollup-plugin-commonjs-alternate
INSTALL
IMPORT
SIG · ROLLUP-PLUGIN-COMM
R
rollup-plugin-commonjs-alternate
devopsjavascriptv0.8.0
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–223 runs
build_error
glibc
node 18–223 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

commonjs
✓ import commonjs from 'rollup-plugin-commonjs-alternate'
✗ import { commonjs } from 'rollup-plugin-commonjs-alternate'
Default export only; named export is incorrect.
commonjs (require)
✓ const commonjs = require('rollup-plugin-commonjs-alternate')
✗ const { commonjs } = require('rollup-plugin-commonjs-alternate')
CJS require gives the default function directly; destructuring is wrong.
Plugin with options
✓ commonjs({ namedExports: { 'react': ['Component'] } })
✗ commonjs({ namedExports: { 'react': { Component: 'Component' } } })
namedExports is an object mapping module ID to array of exported names, not a map of exports.

Demonstrates configuring the plugin with namedExports, extensions, and define options in a Rollup config.

// rollup.config.js import commonjs from 'rollup-plugin-commonjs-alternate'; import resolve from '@rollup/plugin-node-resolve'; import json from '@rollup/plugin-json'; export default { input: 'src/index.js', output: { file: 'dist/bundle.js', format: 'iife' }, plugins: [ resolve(), commonjs({ namedExports: { 'node_modules/react/index.js': ['Component', 'createElement'] }, extensions: ['.js', '.jsx'], define: { 'process.env.NODE_ENV': JSON.stringify('production') } }), json() ] };
Debug
Known issues
gotchaNamed exports must be explicitly configured; auto-detection does not work.
fix
Use namedExports option to specify exported names per module.
affects: >=0.1.0
gotchaDynamic requires are not converted to static imports; they remain as require calls that may break in the bundle.
fix
Avoid dynamic requires; if needed, handle them manually or use a different plugin.
affects: >=0.1.0
gotchaThe plugin assumes all CommonJS modules are in strict mode; non-strict modules may cause unexpected behavior.
fix
Ensure all CJS modules have 'use strict' or are compliant with strict mode.
affects: >=0.1.0
breakingVersion 0.7.0 changed default extensions from ['.js', '.json'] to ['.js']
fix
If you need .json support, set extensions to ['.js', '.json'] explicitly.
affects: >=0.7.0
Errors
Common errors & fixes
Error: Cannot find module 'react'
The plugin does not resolve modules; it only converts CJS to ESM. You need a resolve plugin.
fix
Add @rollup/plugin-node-resolve before the commonjs plugin in the plugins array.
TypeError: Cannot read properties of undefined (reading 'isModule')
Node module resolution is incomplete; the plugin may not handle certain module formats.
fix
Update to the latest version and add necessary resolve and external plugins.
Upgrade
Version history
0.8.0latest on npm
Audit
Dependencies
estree-walkerrequiredAST traversal
magic-stringrequiredsource code manipulation
resolverequiredmodule resolution
Agent activity
7 hits · last 30 days
node
6
Resources
rollup-plugin-commonjs-alternate — npm install rollup-plugin-commonjs-alternate · libregistry