npm Peer Dependencies (2024)

We’ll assume that you have a basic working knowledge of npm. However, quiteoften people struggle with the different types of dependencies and, in particularPeer Dependencies. If so, this post will improve your understanding. Thereare actually five different dependency types defined by npm:

  • normal dependencies
  • dev dependencies
  • peer dependencies
  • optional dependencies
  • bundled dependencies

Of these, normal and dev dependencies are generally well understood and theuse cases for optional and bundled dependencies are few and far between, whichbrings us nicely to the subject of this post, peer dependencies.

We’ll describe what they are and discuss when it might be appropriate to usethem.

Dependencies vs. Peer Dependencies

If you’ve used nodejs at all, you will have come across normal dependencies inpackage.json. They are used to describe the modules that your application dependson. They will always be included in your built module. They look a bit like this:

And, you’ve probably also seen dev dependencies, which are used to describedependencies that are used as part of your development process but not neededin you product build. They are installed in your dev environment bynpm install but they are not included in your built module.Test and buildtools are commonly in the dev dependencies,for example:

"devDependencies": { "@testing-library/jest-dom": "^5.11.6", "@testing-library/react": "^11.2.2", "@testing-library/user-event": "^12.6.0"}

Peer dependencies effectively declare a dependency without including thedependency in your built module. When an application includes your module,that application will in turn need to include the declared dependency. Withnpm version 4 through to 6, a warning is issued when you run npm installto remind you to install the peer dependencies. Prior to version 4, npmautomatically included peer dependencies if they weren’t explicitly included.That behaviour led to too much complexity in dependency tree calculation andit was dropped in version 4. With npm version 7, a brand new dependency treemanager is being introduced. With this, automatic inclusion of peerdependencies is returning. You can read about the Arborist dependency treemanager here.

When to Use Peer Dependencies

Sometimes, and particularly when you are building a library that will be usedby other applications, you will have a dependency that will almost certainlyalso be a core dependency of those other applications. For example, if you arebuilding a library of React components, React will be a dependency you need,but almost certainly the application that uses your library will need React. Thisis where we use Peer Dependencies.

Peer dependencies provide the details of what the host application is expectedto provide. Taking our React example, our peer dependencies might look likethis:

"peerDependencies": { "react": "^17.0.1", "react-dom": "^17.0.1"}

Semantic Versioning

When specifying the allowed versions of a package in a peer dependency in ourpackage.json, we often want to specify more liberal version ranges than wetypically use for normal or dev dependencies. As a brief recap, semanticversioning or semver is used in package.json to specify the versions of adependency that are compatible with the package described by the package.json.Semantic versioning generally defines the version of a package using threedigits, major.minor.patch.

Most normal or dev dependencies use one of two specifiers:

  • tilde (~) to allow newer patch level versions of a package
  • caret (^) to allow newer minor level versions of a package

Caret is the default when we use npm install or npm install --save-dev.The npm option --save-prefix can be used to change the specifier used.

To refresh your knowledge of npm’s semver usage, we’d recommend reading theofficial docs.

When it comes to peer dependencies, we are generally specifying the versionsof a dependency that our package can work with, rather than the versionwe’d prefer to use. This sometimes leads to broader specifications than thoseprovided by the tilde and caret specifiers. For example, we might know thatour package can work with more than one major version of a dependency. Considerthat we might be building a React Library that works for both React 16 versionsgreater than minor version 8 an also works with React 17 and is optimisticallyexpected to work with future minor updates to React 17. Our specifier mightlook like this:

"peerDependencies": { "react": "16.8 - 17"}

A useful tool for checking your specifiers is the semver calculator,available on the npm web site.

Unmet Dependencies When Testing

When you use peer dependencies, npm will not automatically install thosedependencies (see comments above in respect to npm version 7). This canlead to errors when you are running tests on your package, although youwill get warnings when executing npm to prompt you to install the peerdependencies. One way to avoid these warnings and errors is to also includethe peer dependencies as dev dependencies. That way, they will be availablefor local testing but will still be peer dependencies in the publishednpm module.

If you’d like to keep up with new posts, please consider following us.

Follow @Fathom_Tech

npm Peer Dependencies (2024)
Top Articles
What Happens If You Make a Lump-Sum Payment on Your Mortgage?
Life In Basel
Devotion Showtimes Near Xscape Theatres Blankenbaker 16
Ups Customer Center Locations
Edina Omni Portal
Noaa Charleston Wv
Belle Meade Barbershop | Uncle Classic Barbershop | Nashville Barbers
How to change your Android phone's default Google account
Acts 16 Nkjv
Craigslist Cars And Trucks Buffalo Ny
Nwi Police Blotter
Cinepacks.store
Max 80 Orl
Large storage units
What’s the Difference Between Cash Flow and Profit?
Caroline Cps.powerschool.com
Ap Chem Unit 8 Progress Check Mcq
Uhcs Patient Wallet
Fairy Liquid Near Me
Kürtçe Doğum Günü Sözleri
Spergo Net Worth 2022
Amih Stocktwits
Daytonaskipthegames
The Tower and Major Arcana Tarot Combinations: What They Mean - Eclectic Witchcraft
Form F-1 - Registration statement for certain foreign private issuers
Coomeet Premium Mod Apk For Pc
Integer Division Matlab
Ltg Speech Copy Paste
Idle Skilling Ascension
Kroger Feed Login
Black Panther 2 Showtimes Near Epic Theatres Of Palm Coast
100 Gorgeous Princess Names: With Inspiring Meanings
Mercedes W204 Belt Diagram
Chris Provost Daughter Addie
Ket2 Schedule
Wsbtv Fish And Game Report
Eastern New Mexico News Obituaries
What Is Kik and Why Do Teenagers Love It?
Gary Lezak Annual Salary
Electronic Music Duo Daft Punk Announces Split After Nearly 3 Decades
2007 Peterbilt 387 Fuse Box Diagram
Let's co-sleep on it: How I became the mom I swore I'd never be
Fairbanks Auto Repair - University Chevron
Ssc South Carolina
Craigslist Houses For Rent Little River Sc
15 Best Places to Visit in the Northeast During Summer
Backpage New York | massage in New York, New York
The Plug Las Vegas Dispensary
Pelican Denville Nj
Zalog Forum
Morgan State University Receives $20.9 Million NIH/NIMHD Grant to Expand Groundbreaking Research on Urban Health Disparities
32 Easy Recipes That Start with Frozen Berries
Latest Posts
Article information

Author: Otha Schamberger

Last Updated:

Views: 6293

Rating: 4.4 / 5 (55 voted)

Reviews: 94% of readers found this page helpful

Author information

Name: Otha Schamberger

Birthday: 1999-08-15

Address: Suite 490 606 Hammes Ferry, Carterhaven, IL 62290

Phone: +8557035444877

Job: Forward IT Agent

Hobby: Fishing, Flying, Jewelry making, Digital arts, Sand art, Parkour, tabletop games

Introduction: My name is Otha Schamberger, I am a vast, good, healthy, cheerful, energetic, gorgeous, magnificent person who loves writing and wants to share my knowledge and understanding with you.