Just using my blog to share a little tip for future me that current me keeps forgetting. Over the years when working with clients 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.
READER DISCOUNTSave $50 on terminal.training
I've published 38 videos for new developers, designers, UX, UI, product owners and anyone who needs to conquer the command line today.
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 installs 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 👍