question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Struggling to set up a monorepo with 2 RN apps and a shared package that includes native code

See original GitHub issue

Ask your Question

Sorry for the impending wall of text, and thank you in advance for any help.

I have a monorepo (using yarn workspaces) that has several non-rn apps (a graphql server, 2 different websites, others) and as of now a single react-native ios/android app. This works by just nohoisting everything RN related, and some metro config to make sure it can find the js in the other packages. Everything related I’ve found (mostly issues in this repo, honestly) is more about getting a single RN app to work in a monorepo.

I’m looking for help with configuring everything to make this work. This is rapidly approaching being a blocker for some of my team, so I can definitely dedicate some decent time to working on cli if needed. My native-fu is somewhat weak, but I’m happy to jump in if I can get some some guidance. I am flexible.

Here is my desired project structure:

.
β”œβ”€β”€ package.json
└── packages
    |
    β”œβ”€β”€ shared (this is shared between everything that wants to use it)
    |   β”œβ”€β”€ package.json
    |   └── js (does not reference native code)
    |
    β”œβ”€β”€ shared-browser
    |   β”œβ”€β”€ package.json
    |   └── js
    |
    β”œβ”€β”€ shared-mobile
    |   β”œβ”€β”€ package.json
    |   └── js (references native code)
    |
    β”œβ”€β”€ mobile-professional (first-mobile-app)
    |   β”œβ”€β”€ package.json
    |   β”œβ”€β”€ ios
    |   β”œβ”€β”€ android
    |   └── js (references native code, shared-mobile, and shared-everywhere)
    |
    β”œβ”€β”€ mobile-business (second-mobile-app)
    |   β”œβ”€β”€ package.json
    |   β”œβ”€β”€ ios
    |   β”œβ”€β”€ android
    |   └── js (references native code)
    |
    β”œβ”€β”€ first-web-app
    |   β”œβ”€β”€ package.json
    |   └── js
    |
    β”œβ”€β”€ second-web-app
    |   β”œβ”€β”€ package.json
    |   └── js
    |
    β”œβ”€β”€ non-js-package
    β”œβ”€β”€ non-js-package
    └── non-js-package

Currently, it’s basically the same as that except that there is only one mobile app, and the shared-mobile folder doesn’t reference any native code.

We’re trying to add a second RN android/ios app to the mix. Currently we have a single shared folder for stuff accessible everywhere. I’m first trying to get the single app to work with the native code hoisted, since that seems prerequisite to the shared code being able to be hoisted. Assuming that works, I’d be able to get stuff working by not having any shared native code, but this involves a ton of code duplication with tedious reference changes.

Ideally, we’d like to add the majority of the native stuff to our shared-mobile folder. I was able to get IOS to work with a bit of a hack, but I’ve been unable to get android working. I’ve been trying both with what ships with RN 0.61.2, as well as upgrading the cli to the latest 3 alpha (7?).

For the ioS stuff, to get it working, I simply updated the xcode project and podfile to look for stuff at ../../../node_modules instead of ../node_modules, and then I duplicated the .command file that gets opened in a new window and added a step to CD into the packages/mobile-professional folder (otherwise it runs in the wrong directory and metro doesn’t pick up the config that I’ve got inside that folder).

For the android stuff, I first adjusted the build.grade, app/build.gradle and settings.graddle to look up 2 directories more than before, which gets me farther but then i get A problem occurred evaluating settings ... Text must not be null or empty. I’ve tried various react-native.config.js values in both the root and my folder (and pointing the android dir within both to ./android and ./packages/mobile-professional/android) but that does not work with the old cli or the new. If I modify the native_modules.gradle file that gets installed as part of cli, (or the android subcomponent), so that it runs react-native config in the mobile folder rather than the root, I finds what it wants, but then I get some build errors about not being able to find android.support.annotations.nullable in one of the native components, saying more specifically that android.support.annotations doesn’t exist, which feels like a linking issue.

Just to make sure it’s not a hangup, re: react-native.config.js, i’ve also moved the limited rnpm stuff (just assets) in there as well out of the package.json

I’m trying to set up an example repo, as ours is closed and stripping things out has proven time consuming (we have a lot of process going on, everything is built in reasonml and compiled with bucklescript down to JS). In the meantime I’m posting original versions of my build and config files, and new versions.

app-build.gradle-modified.txt app-build.gradle-original.txt build.gradle-modified.txt build.gradle-original.txt metro.config.js.txt mobile-professional-package.json.txt rn-cli.config.js.txt settings.gradle-modified.txt settings.gradle-original.txt

Sorry again for the huge wall of text, but I figured it was better to provide everything relevant rather than too little.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:20 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
maxkorpcommented, Jun 25, 2020

Here is an example repo I set up that shows what we’ve done to get things working

https://github.com/maxkorp/codepush-monorepo-example. There are some commits on master that are related to similar issues for codepush, but you can go back to the second commit to see how I got things working.

1reaction
Minishlinkcommented, Jun 23, 2020

Here’s a probable workaround, make sure you have the same cli and RN version. I don’t give any warranty on this. If you use this, test it thoroughly in debug/release mode on Android/iOS. Also make sure to understand how patch-package work. https://gist.github.com/Minishlink/14fde025b4352d0802e25e098e357046

Read more comments on GitHub >

github_iconTop Results From Across the Web

Setting up a Monorepo with React Native You.I and Yarn - G2i
Our goal is to have two apps in the same repository, and allow these two apps to use the same modules inside a...
Read more >
A Guide to Monorepos for Front-end Code - Toptal
A monorepo or monorepository is a code management and architectural concept whereby you keep all your isolated bits of code inside one super...
Read more >
Sharing TypeScript Code Between Web and React Native
In this post, I'll describe how we handled it. Plan A. Let's call the existing web app web-monorepo and the new React Native...
Read more >
Sharing code between web and native projects of React in a ...
In this article, we will create a monorepo containing a basic react app, a react native app, and a common shared project, which...
Read more >
Monorepos in JavaScript and TypeScript - Hacker News
What I wanted most of all was a repo where both frontend code and backend code were based on the ... yarn create...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found