question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using styled-components in a monorepo

See original GitHub issue

Version

> 2.0

Reproduction

We have a UI library built with styled-components that is for use by our team internally as well as with external developers. While this library depends on styled-components, it does not bundle styled components as part of the npm distribution.

Steps to reproduce

Install an npm library that exports components built with styled-components in a React project that also uses styled-component (Create React App in our case)

Expected Behavior

styled-components should work for both the “local” React app and the imported library components.

Actual Behavior

As soon as the library components are imported to the local React app, the local apps styled-components stop working.

I’m guessing this is related to #1032. The temporary fix we’ve found is to downgrade to 1.4.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:22
  • Comments:43 (16 by maintainers)

github_iconTop GitHub Comments

14reactions
christiannathscommented, May 22, 2018

For anyone else landing running into this and landing here, this part of the Styled Components FAQ solved it for me: https://www.styled-components.com/docs/faqs#duplicated-module-in-node_modules

Specifically:

  • setting styled-components as a peerDependency in my react library
  • updating the Webpack config in my react app to prioritize it’s own node_modules folder

I’m using create-react-app and react-app-rewired, so my config-overrides.js file in my react app now looks like this:

const path = require('path');

module.exports = function override(config, env) {

  config.resolve = Object.assign({}, config.resolve, {
    modules: [path.resolve(__dirname, 'node_modules'), 'node_modules'],
  });

  return config;
};

Hope that helps someone else.

6reactions
wmdmarkcommented, Jun 27, 2018

@christiannaths I dunno if I’d see this as a CRA issue… most build tools are trending in the direction of zero config. It seems to me that it would be best if styled-components didn’t require manually configuring Webpack to behave as expected in a library scenario. I don’t fully understand the underlying issue here but styled-components pre 2.0 worked just fine without any special configuration as do other CSS and JS libraries like glamorous etc. It’s a bummer styled-components doesn’t.

Read more comments on GitHub >

github_iconTop Results From Across the Web

FAQs
You have a monorepo structure for your project (e.g, lerna, yarn workspaces) and styled-components module is a dependency in more than one package...
Read more >
Creating a Monorepo with Lerna & Yarn Workspaces
We'll be using React and styled-components to develop our UI components, so let's install those first. $ yarn add --dev -W react react-dom ......
Read more >
Share typescript module with every package in a monorepo
I have a monorepo of react components that are built using styled-components, getting their themes from a theme provider.
Read more >
Support Multiple Frameworks in a Monorepo
We're trying to build a single button component that uses just one ... and import the CSS Modules styles into our React component...
Read more >
Amsterdam Styled Components (ASC)
We aim the components to be aligned with the Amsterdam Design System. ... A monorepo: one source for styled components and everything that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found