styled-components: TypeError: Cannot read properties of undefined (reading 'withConfig')
Environment
System:
- OS: macOS 13.2
- CPU: (10) arm64 Apple M1 Pro
- Memory: 2.16 GB / 32.00 GB
- Shell: 5.8.1 - /bin/zsh
Binaries:
- Node: 18.13.0 - ~/.nvm/versions/node/v18.13.0/bin/node
- Yarn: 3.3.1 - ~/.nvm/versions/node/v18.13.0/bin/yarn
- npm: 8.19.3 - ~/.nvm/versions/node/v18.13.0/bin/npm
npmPackages:
- styled-components: ^5.3.6 => 5.3.6
Reproduction
I’m making a ui library (including Flex, Stack, etc.) that wraps styled-components called @jsxcss/styled-components. This library works fine with create-react-app.
However, in the Next.js environment, Server Error It doesn't work well with the error TypeError: Cannot read properties of undefined (reading 'withConfig').
This link reproduces this error situation.
You can see the code of @jsxcss/styled-components at this link.

Steps to reproduce
- You can clone repository in This link reproduces this error situation.
- npm install
- npm run dev
- check error in http://localhost:3000
Expected Behavior
No server error.
Actual Behavior
Error like that picture
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 3
- Comments: 16
@manudeli I see you’re using Rollup. I just ran into the same issue with my library while upgrading from Rollup v2 to v3.
In my case I had to add the
output.interopoption and set it to"auto":They changed some defaults in v3 as outlined in the migration guide. By the way,
"compat"also seems to work.use import statement as:
import {styled} from styled-components;Does someone found a solution to this? changing interop does nothing here. I have that problem only in vites SSR build. Not in the website build.
I am using vite to build a library with the library-mode and seeing empty theme , similar to above issue. Dev mode works fine. Tried all approaches mentioned above but no luck
I ran into a very similar issue: theme is
undefinedbutThemeProvideris definitely being passed a non-empty theme.In my case, the issue was that
ThemeProviderwas created in a separate package from where the theme was being referenced, and the packages were using different versions of react-dom!After explicitly adding
react-dom@18to package B’s package.json, the issue was magically fixed.I’m guessing your issue is somewhat related. Maybe the places where you’re setting and referencing the theme have different views of the DOM, or are using different react-dom versions?
Same problem using rollup for building a library
Your approach has indeed solved this problem