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.

image

Steps to reproduce

  1. You can clone repository in This link reproduces this error situation.
  2. npm install
  3. npm run dev
  4. 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

Most upvoted comments

@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.interop option and set it to "auto":

// rollup.config.js
export default {
  // ...
  output: {
    interop: '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 undefined but ThemeProvider is definitely being passed a non-empty theme.

In my case, the issue was that ThemeProvider was created in a separate package from where the theme was being referenced, and the packages were using different versions of react-dom!

// package A, using react-dom@18
const App = () => (
  <ThemeProvider theme={theme}>
    <GlobalStyle />
    <p>hello</p>
  </ThemeProvider>
);

// package B, using react-dom@17 implicitly
const GlobalStyle = createGlobalStyle`
  body {
    font-family: ${props => props.theme.fontFamily};
  }
`

After explicitly adding react-dom@18 to 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?

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

Same problem using rollup for building a library

use import statement as:

import {styled} from styled-components;

Your approach has indeed solved this problem