{"id":21957,"library":"rollup-plugin-commonjs-alternate","title":"rollup-plugin-commonjs-alternate","description":"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.","status":"maintenance","version":"0.8.0","language":"javascript","source_language":"en","source_url":null,"tags":["javascript"],"install":[{"cmd":"npm install rollup-plugin-commonjs-alternate","lang":"bash","label":"npm"},{"cmd":"yarn add rollup-plugin-commonjs-alternate","lang":"bash","label":"yarn"},{"cmd":"pnpm add rollup-plugin-commonjs-alternate","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"AST traversal","package":"estree-walker","optional":false},{"reason":"source code manipulation","package":"magic-string","optional":false},{"reason":"module resolution","package":"resolve","optional":false}],"imports":[{"note":"Default export only; named export is incorrect.","wrong":"import { commonjs } from 'rollup-plugin-commonjs-alternate'","symbol":"commonjs","correct":"import commonjs from 'rollup-plugin-commonjs-alternate'"},{"note":"CJS require gives the default function directly; destructuring is wrong.","wrong":"const { commonjs } = require('rollup-plugin-commonjs-alternate')","symbol":"commonjs (require)","correct":"const commonjs = require('rollup-plugin-commonjs-alternate')"},{"note":"namedExports is an object mapping module ID to array of exported names, not a map of exports.","wrong":"commonjs({ namedExports: { 'react': { Component: 'Component' } } })","symbol":"Plugin with options","correct":"commonjs({ namedExports: { 'react': ['Component'] } })"}],"quickstart":{"code":"// rollup.config.js\nimport commonjs from 'rollup-plugin-commonjs-alternate';\nimport resolve from '@rollup/plugin-node-resolve';\nimport json from '@rollup/plugin-json';\n\nexport default {\n  input: 'src/index.js',\n  output: { file: 'dist/bundle.js', format: 'iife' },\n  plugins: [\n    resolve(),\n    commonjs({\n      namedExports: {\n        'node_modules/react/index.js': ['Component', 'createElement']\n      },\n      extensions: ['.js', '.jsx'],\n      define: {\n        'process.env.NODE_ENV': JSON.stringify('production')\n      }\n    }),\n    json()\n  ]\n};","lang":"javascript","description":"Demonstrates configuring the plugin with namedExports, extensions, and define options in a Rollup config."},"warnings":[{"fix":"Use namedExports option to specify exported names per module.","message":"Named exports must be explicitly configured; auto-detection does not work.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Avoid dynamic requires; if needed, handle them manually or use a different plugin.","message":"Dynamic requires are not converted to static imports; they remain as require calls that may break in the bundle.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Ensure all CJS modules have 'use strict' or are compliant with strict mode.","message":"The plugin assumes all CommonJS modules are in strict mode; non-strict modules may cause unexpected behavior.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"If you need .json support, set extensions to ['.js', '.json'] explicitly.","message":"Version 0.7.0 changed default extensions from ['.js', '.json'] to ['.js']","severity":"breaking","affected_versions":">=0.7.0"}],"env_vars":null,"last_verified":"2026-04-27T00:00:00.000Z","next_check":"2026-07-26T00:00:00.000Z","problems":[{"fix":"Add @rollup/plugin-node-resolve before the commonjs plugin in the plugins array.","cause":"The plugin does not resolve modules; it only converts CJS to ESM. You need a resolve plugin.","error":"Error: Cannot find module 'react'"},{"fix":"Update to the latest version and add necessary resolve and external plugins.","cause":"Node module resolution is incomplete; the plugin may not handle certain module formats.","error":"TypeError: Cannot read properties of undefined (reading 'isModule')"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}