← Back to Blog

Understanding peerDependencies in package.json

2020-12-19·1 min read
ReactReact NativeJavaScriptNetworking

I didn't know about peerDependencies until after I created a react native library myself. I've seen it before, but didn't really know or care why it was written in some of package.json file.

By adding peerDependecies object in the package.json file in a library that we created, means that the other project that's using this library needs to install the other library that's listed as its dependencies. For example we add peerDependencies in a react-native-theme library like the example below:

peerDependecies: {
  "@react-native-async-storage/async-storage": "~1.13.2",
}

And the Demo project that is using react-native-theme as one of it's dependencies, also need to add async-storage as its dependency. The package.json file inside Demo project will looks like this

dependencies: {
  "react-native-theme": "1.0.0",
  "@react-native-async-storage/async-storage": "1.13.2",
}

And it's recommended to use semantic versioning to determine the peer dependency requirements. It's either something like ~1.0 or >=1.2.0<2.

Thanks for reading! :smile: