sections/faqs/duplicated-styled-components.mdx
If you are seeing a warning message in the console like the one below, you probably
have several instances of styled-components initialized on the page.
It looks like there are several instances of "styled-components" initialized in this application. This may cause dynamic styles not rendering properly, errors happening during rehydration process and makes you application bigger without a good reason.
If you are using a building tool like webpack, consider checking your bundle for duplication of the "styled-components" module.
This may cause dynamic styles not working properly or even errors during rehydration if you are using server-side rendering.
There are several common reasons for this to happen:
styled-components running on the same page
(e.g., several entry points in webpack are loaded on the same page)styled-components library somewhere in your dependenciesstyled-components
module is a dependency in more than one package (this one is more or less the same as the previous one)If you have several applications running on one page, consider using one styled-components module for all of them. If you are using webpack, you can use CommonsChunkPlugin
to create an explicit vendor chunk,
that will contain the styled-components module:
module.exports = {
entry: {
+ vendor: ["styled-components"],
app1: "./src/app.1.js",
app2: "./src/app.2.js",
},
plugins: [
+ new webpack.optimize.CommonsChunkPlugin({
+ name: "vendor",
+ minChunks: Infinity,
+ }),
]
}
node_modulesIf you think that the issue is in duplicated styled-components module somewhere in your dependencies, there are several ways to check this. You can use npm ls styled-components, yarn list --pattern styled-components or find -L ./node_modules | grep /styled-components/package.json commands in your application folder.
If none of these commands identified the duplication, try analyzing your bundle for multiple instances of styled-components. You can just check your bundle source, or use a tool like source-map-explorer
or webpack-bundle-analyzer.
If you identified that duplication is the issue that you are encountering there are several things you can try to solve it:
If you are using npm you can try running npm dedupe. This command searches the local dependencies and tries to simplify the structure by moving common dependencies further up the tree.
Be aware that
npm dedupedoesn't work well with symlinked folders (i.e., when you usenpm link)
If you are using webpack, you can change the way it will resolve
the styled-components module. You can overwrite the default order in which webpack will look for your dependencies and make your application node_modules more prioritized than default node module resolution order:
resolve: {
+ alias: {
+ "styled-components": path.resolve(appFolder, "node_modules", "styled-components"),
+ }
}
One possible fix to get styled-components to run in a Lerna monorepo across packages, is to hoist shared dependencies to the root of your monorepo file. Try running the bootstrap option with the --hoist flag.
lerna bootstrap --hoist
Alternatively, you can remove styled-components from your package.json file and hoist it manually to your top-level package.json file.
Example of a package.json file in a Lerna root folder
{
"name": "my-styled-monorepo",
"devDependencies": {
"lerna": "3.6.0"
},
"dependencies": {
"styled-components": "3.4.5"
},
"scripts": {
"bootstrap": "lerna bootstrap",
"clean": "lerna clean",
"start": "lerna run start",
"build": "lerna run build"
}
}