[plugin-commonjs]: "Error: 'useState' is not exported by node_modules/react/index.js, imported by index.jsx;"
See original GitHub issue- Rollup Plugin Name: @rollup/plugin-commonjs
- Rollup Plugin Version:^21.0.1
- Rollup Version:^2.53.2
- Operating System (or Browser):MacOS Catalina 10.15.7 Chrome v93.0.4577.82
- Node Version:v14.17.6
- Link to reproduction (⚠️ read below): https://replit.com/join/eitlhtkdna-garethchetwood
Expected Behavior
The project builds successfully
Actual Behavior

Additional Information
I’ve created a small example with a few components and mostly similar config to mine. Just go to the repl link and do npm install; npm run build.
I’ve looked around about this error and the solution seems to be to use namedExports but that is now deprecated. Another solution seems to be to update to the latest rollup and commonjs plugin but I have done that and I’m still getting the error 😕
Could be something I’ve done wrong in my config as I’ve been messing around with it a lot, but I’ve seen a few people flag up this exact issue, less a qualifying reproduction, so I thought I’d take a crack at it.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:6 (5 by maintainers)
Top Results From Across the Web
'rollup-plugin-node-resolve' can't work with `useState` of react ...
As the answer link provided by @Tholle, the problem is rollup-plugin-commonjs is not able to find the setState export from react, ...
Read more >React useState Hook - W3Schools
To use the useState Hook, we first need to import it into our component. ... Notice that we are destructuring useState from react...
Read more >Trying Rollup for React Applications | by Nathan Sebhastian
@rollup/plugin-commonjs so that Rollup can import code in CommonJS module ... and create an index.js file to render your React application:
Read more >Bundle your React app with Rollup.js : r/reactjs - Reddit
Error No #3, name exports, this time. Let's fix it. For this we need the @rollup/plugin-commonjs plugin which converts CommonJS modules to ES6....
Read more >Importing useState - not available for all projects? - React.js
import React, { useState } from 'react';. Can someone confirm this for me? I don't think issue was from an error. I was...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
So to refine my initial suggestion:
requireexpressions insteadThe advantage would be that this requires no additional setup by the user and my hope is that it would “just work”. Question is, though, if this is easily feasible. I could also look into setting up a PR for this.
If there is an
importin a file, it is no longer a CommonJS file. While the commonjs plugin now supports “mixed” modules with both imports and requires, for those modules it will only convertrequireexpressions but ignoreexportsandmodule.exportsfor exports.The problem is: If the file contains non-JS syntax like JSX expressions, which is of course really common for React projects, then there needs to be a Babel instance BEFORE the commonjs plugin to convert the JSX syntax to plain JavaScript for the commonjs plugin to understand.
So for React, this is actually a really common issue:
Another solution of course would be to run Babel twice, once before commonjs to convert just the JSX (and hopefully not need any imported helpers, or rather, inline the helpers), and once after that with shared helpers. But this would likely have quite a performance overhead, not mentioning that it is really complicated.