Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Next.js and React

Rollup cannot find a default export in React's CommonJS entry file

Your error

"default" is not exported by "node_modules/react/index.js", imported by "src/App.js".2 more lines it can show

Fix it fast

Most common cause, 1 of 3

No CommonJS plugin translates React's entry

Install the scoped plugins, remove the old one if it is still listed, and add both to the config. node-resolve finds the package in node_modules; commonjs turns its CommonJS code into something Rollup can bundle.

Fix
npm uninstall rollup-plugin-commonjs
npm install --save-dev @rollup/plugin-commonjs @rollup/plugin-node-resolve

// rollup.config.mjs
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';

export default {
  input: 'src/main.js',
  output: { file: 'dist/bundle.js', format: 'es' },
  plugins: [commonjs(), resolve()],
};
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

Rollup 4 reports "default" is not exported by "node_modules/react/index.js" (Rollup 2 printed 'default' in single quotes) because React 19 still publishes that entry as CommonJS, while Rollup reads only ES module syntax by itself. Add @rollup/plugin-commonjs beside @rollup/plugin-node-resolve, keep node_modules inside its include, or mark react as external in a component library.

  • Applies to Rollup 4.68, Rollup 2.79 (old wording), @rollup/plugin-commonjs 29, @rollup/plugin-node-resolve 16, React 19.3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Print the plugins and the external list from your Rollup config, then the versions of Rollup and its two module plugins. A config with no commonjs entry, or one that still loads the old rollup-plugin-commonjs package, already accounts for the error.

Run this first
grep -n "commonjs\|resolve\|external\|include" rollup.config.*
npm ls rollup @rollup/plugin-commonjs @rollup/plugin-node-resolve rollup-plugin-commonjs

Then find the line you see in this list and open its fix:

  1. 1No CommonJS plugin translates React's entryThe plugins array has no commonjs() call, or the import comes from the deprecated rollup-plugin-commonjs
  2. 2The commonjs include pattern leaves React outcommonjs() is in the config, but with an include list such as 'src/**' or a node_modules/** glob in a monorepo
  3. 3A component library bundles React instead of leaving it externalThe bundle is a package for other apps, react is a peer dependency, and the config has no external list

What does the error look like?

  • "default" is not exported by "node_modules/react/index.js", imported by "src/App.js".
  • 'default' is not exported by node_modules/react/index.js, imported by src/App.js
  • "useState" is not exported by "node_modules/react/index.js", imported by "src/Counter.js".

Cause 1 of 3

No CommonJS plugin translates React's entry

You seeThe plugins array has no commonjs() call, or the import comes from the deprecated rollup-plugin-commonjs

Check

Rollup says default is not exported by node_modules/react/index.js when nothing converts that file: it fills module.exports with a require call and has no export statement, and Rollup only understands ES module syntax unless a plugin translates the rest. Rollup's troubleshooting guide sends you to @rollup/plugin-commonjs, which detects the named exports on its own, in place of the deprecated rollup-plugin-commonjs.

Check
head -n 8 node_modules/react/index.js

Fix

Install the scoped plugins, remove the old one if it is still listed, and add both to the config. node-resolve finds the package in node_modules; commonjs turns its CommonJS code into something Rollup can bundle.

Fix
npm uninstall rollup-plugin-commonjs
npm install --save-dev @rollup/plugin-commonjs @rollup/plugin-node-resolve

// rollup.config.mjs
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';

export default {
  input: 'src/main.js',
  output: { file: 'dist/bundle.js', format: 'es' },
  plugins: [commonjs(), resolve()],
};

Verify

The build finishes, writes dist/bundle.js and no longer prints the not-exported message for react.

Verify
npx rollup -c

Confirmed by rollupjs.org

Did this fix it?

Cause 2 of 3

The commonjs include pattern leaves React out

You seecommonjs() is in the config, but with an include list such as 'src/**' or a node_modules/** glob in a monorepo

Check

The same not-exported message can appear with the plugin installed when its include option is narrowed. Left unset, include covers every .js and .cjs file; a list of patterns restricts the plugin to what matches, so node_modules/react/index.js passes through untranslated. In a monorepo or after npm link, the real path of the React copy can also sit outside the local node_modules folder that a glob names.

Check
grep -n -A3 "commonjs(" rollup.config.*

Fix

Delete the include option, or use the regular expression that the plugin's README suggests, which matches a node_modules folder wherever it is.

Fix
commonjs({
  include: /node_modules/,
}),

Verify

Run the build again; the bundle now contains React's code, or the import is gone if react is external.

Verify
npx rollup -c

Confirmed by github.com

Did this fix it?

Cause 3 of 3

A component library bundles React instead of leaving it external

You seeThe bundle is a package for other apps, react is a peer dependency, and the config has no external list

Check

When the build is a component library, the not-exported error for react/index.js shows that React is being pulled into your package at all. The app that installs the library supplies its own React. Ids in Rollup's external option stay outside the bundle, so the import is kept as written and React's CommonJS file is never read.

Check
grep -n -A4 "peerDependencies" package.json

Fix

List React and its subpath imports as external. The plugins can stay for any other CommonJS dependency.

Fix
// rollup.config.mjs
export default {
  input: 'src/index.js',
  output: { file: 'dist/index.js', format: 'es' },
  external: ['react', 'react-dom', 'react/jsx-runtime'],
  plugins: [commonjs(), resolve()],
};

Verify

The output keeps a plain import from react and holds none of React's own source.

Verify
grep -n "from 'react'" dist/index.js

Confirmed by rollupjs.org

Did this fix it?

How do I stop it coming back?

  • Start every Rollup config that touches npm packages with @rollup/plugin-node-resolve and @rollup/plugin-commonjs, and pin both in devDependencies.
  • In library packages keep react and react-dom in peerDependencies and in external together, so the two lists cannot drift apart.
  • Run the Rollup build in CI on every dependency upgrade; a missing export stops it with the error code MISSING_EXPORT before a broken bundle ships.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: TaxiDex, QistLock, Taskvare.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.