Transitive Dependencies and React Native Autolinking

Tyler
Red Shift
Published in
2 min read1 day ago

Buckle up because it’s time to talk about one of the biggest hot-button topics in React Native tech.

Buckle up sign, Florida Turnpike. Photo by Infrogmation. License Creative Commons Attribution-Share Alike 2.0 Generic License

You’re gonna want to read all the way to the end because everyone is buzzing about…

React Native autolinking and transitive dependencies?

Ok, so maybe this isn’t a groundbreaking think piece. But it’s relevant to new React Native library authors, who may be asking themselves: “why don’t native modules get bundled together as dependencies in React Native, the way they do in JavaScript?”

The answer hasn’t changed much since it was addressed in this GitHub comment by Brent Vatne, but if you’re looking for a more recent answer to the question, I hope this article is clarifying and up-to-date. Here’s the answer:

React Native will not autolink transitive dependencies. That means if you’re writing a React Native library and you want to include a native module from another third-party library, you’ll need to specify that library as a peer dependency, and any consumer of your library will need to follow the peer dependency’s installation steps.

This isn’t just an arbitrary rule. It has to do with how native modules and auto linking work in the npm and React Native ecosystem:

Pros

  1. Native modules that need to be linked usually expose some platform capability that consuming apps will want to use independent of your package.
  2. If multiple packages rely on the same transitive dependency, consuming apps have more control over resolving the correct version for their use case.
  3. This approach prevents autolinking from running twice, which would cause an error.

Cons

  1. As a library author, you need to be mindful of what packages you rely on, and if they include native-level code that requires linking.
  2. If you do have dependencies in your library that require linking, you need to manage documentation that explains the situation to your users.

So if anyone ever asks you “hey, why do I need to install Reanimated as a peer dependency? Can’t that other library just package it up?” — send them here. There are good reasons for the decision, but it certainly trips people up from time to time.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Published in Red Shift

The official Infinite Red publication for React Native design & development. We’re a fully distributed team building world-class apps for over 20 years for clients all around the world.

Written by Tyler

Senior Software Engineer at Infinite Red. Maintainer MobX-State-Tree.

No responses yet

Write a response