npm peer dependency error installing packages with React

4 replies
Last updated: Jan 25, 2023
Hello everyone!
Someone please help me to resolve this problem, I am getting this type of error when installing any package. I tried to install
styled-components
I got this error on stack-overflow I saw some people saying just lower the react version then the error will resolved but It didn't resolve my problem. So I uninstalled and used CSS3 for styling. But Now I want to add icons and I am using
react-icons
library again it is showing same type of error.
Thanks In Advance!
AI Update

I understand you're hitting ERESOLVE peer dependency errors when trying to install packages like styled-components and react-icons. This is a common issue in 2025, especially if you're working with React 19, which was recently released and many popular npm packages haven't fully updated their peer dependency declarations yet.

Why This Happens

The error occurs because:

  • React 19 is very new (released late 2024)
  • Many packages like styled-components and react-icons explicitly declare peer dependencies for React 16, 17, or 18
  • npm 7+ enforces peer dependencies strictly by default, causing installation to fail

Solutions (in order of preference)

1. Use --legacy-peer-deps flag (Quickest fix)

npm install styled-components --legacy-peer-deps
npm install react-icons --legacy-peer-deps

This tells npm to ignore peer dependency conflicts. The packages will likely work fine despite the warnings, since React 19 maintains good backward compatibility.

2. Use --force flag

npm install styled-components --force

This overrides the peer dependency check. More aggressive than --legacy-peer-deps but works when that doesn't.

3. Set it globally in .npmrc

Create or edit .npmrc in your project root:

legacy-peer-deps=true

This applies the flag to all future installs automatically.

4. Wait for package updates or use compatible versions

  • styled-components v6 has better React 19 support than v5
  • Check if react-icons has a newer version that supports React 19
  • Alternatively, temporarily downgrade to React 18 if you don't need React 19 features yet

5. For Sanity Studio projects specifically

If you're using Sanity Studio v4, note that it requires Node.js 20+ and React 18+. Make sure there aren't conflicting versions by checking:

npm ls styled-components
npm ls react

If you see multiple versions, that could be the root cause. Sanity Studio internally uses styled-components, so version conflicts can cause issues.

My Recommendation

Start with --legacy-peer-deps for both packages. React 19's backward compatibility is excellent, and these packages will almost certainly work fine. The peer dependency declarations just haven't been updated yet because React 19 is so new.

npm install styled-components react-icons --legacy-peer-deps

Then test your app thoroughly. If everything works (which it likely will), you're good to go! If you continue having issues, check your React version with npm ls react to confirm what you're actually running.

Show original thread
4 replies
That looks like an NPM 7+, peer dependency-related issue. If you’re running NPM 7 or greater, then you could try
npm install --legacy-peer-deps
. See: https://stackoverflow.com/questions/66239691/what-does-npm-install-legacy-peer-deps-do-exactly-when-is-it-recommended-wh
Ideally though, all of the NPM packages that you’re installing should update themselves to be more compatible with the current peer dependency resolution algorithm.
Is anything actually broken in your app? These are just warnings, not errors, so you may be safe to ignore them, although I’d do some thorough testing and investigating first. These errors will hopefully go away as the various package authors publish newer versions.
Thanks
user F
🙏, It was helpful.

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?