rollup-plugin-cjs-check

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

A Rollup plugin that automatically verifies CommonJS output compatibility with Node.js's cjs-module-lexer. Currently at v1.0.3, with stable release cadence. It analyzes the generated CJS bundle and compares the exports detected by Rollup against those that cjs-module-lexer would detect, flagging mismatches that cause broken named imports in Node.js ESM. Unlike manual testing, this provides automated CI enforcement. Requires Rollup >=1.20.0 and Node >=14.0.0.

error RollupError: [rollup-plugin-cjs-check] Export 'MyExport' is not detected by cjs-module-lexer
cause Rollup detected an export that cjs-module-lexer cannot parse, likely due to dynamic export patterns or re-exports.
fix
Use static export syntax or output format 'es' for full ESM compatibility.
error Error: Expected output format to be 'cjs' but got 'es'
cause Plugin was used with a non-CJS output format, causing the check to be skipped (but may warn).
fix
Ensure the Rollup configuration uses format: 'cjs'.
gotcha Plugin only works when output format is 'cjs'. Using with other formats silently skips checking.
fix Ensure your Rollup output format is 'cjs'.
gotcha Plugin does not fix mismatched exports; it only reports them. Manual adjustment of Rollup configuration or code may be needed.
fix Review the reported mismatches and adjust named exports or use 'output.exports: 'named'' configuration.
npm install rollup-plugin-cjs-check
yarn add rollup-plugin-cjs-check
pnpm add rollup-plugin-cjs-check

Basic Rollup configuration using the cjs-check plugin to validate CJS exports.

// rollup.config.js
import cjsCheck from 'rollup-plugin-cjs-check';

export default {
  input: 'src/index.js',
  output: {
    dir: 'output',
    format: 'cjs',
  },
  plugins: [
    cjsCheck(),
  ],
};