Just using my blog to share a little tip for future me that current me keeps forgetting. Over the years when [working with clients](/work) I’ve created reusable React components, and it’s pretty important to me to ensure that dependencies like React aren’t dependencies of my own work - specifically to prevent React (for instance) being included multiple times in my projects.
So…
[](https://training.leftlogic.com/buy/terminal/cli2?coupon=BLOG\&utm_source=blog\&utm_medium=banner\&utm_campaign=remysharp-discount)
[READER DISCOUNTSave $50 on terminal.training](https://training.leftlogic.com/buy/terminal/cli2?coupon=BLOG\&utm_source=blog\&utm_medium=banner\&utm_campaign=remysharp-discount)
[I’ve published 38 videos for new developers, designers, UX, UI, product owners and anyone who needs to conquer the command line today.](https://training.leftlogic.com/buy/terminal/cli2?coupon=BLOG\&utm_source=blog\&utm_medium=banner\&utm_campaign=remysharp-discount)
[$49 - only from this link](https://training.leftlogic.com/buy/terminal/cli2?coupon=BLOG\&utm_source=blog\&utm_medium=banner\&utm_campaign=remysharp-discount)
The first part of this (reasonably) simple solution is to move the dependency from my regular dependency to a peerDependencies
(and as far as I know, I can’t install and save directly to peer deps).
Now when my package is installed npm will warn on the console that React (or some other package) is a peer dependency and needs to be installed.
However (of course), as soon as I npm install …
another package, npm will (un)helpfully remove the peer dependency because npm notices that it’s missing from the direct dependencies.
The solution seems more like a workaround, but it’s to duplicate the peer dependency in both the peerDependencies
and the devDependencies
. That way further `npm install`s won’t remove the required (development) dependency but when the project is published, the published package won’t include the peer dependency.
As I said, one for future me 👍
Published 27-Sep 2018 under #code. [Edit this post](https://github.com/remy/remysharp.com/blob/main/public/blog/peer-deps.md)