Let's make our react-native app work in the browser, the right way.
This tutorial was made for react-native <= 0.61. If you are using a newer ...
For further actions, you may consider blocking this person and/or reporting abuse
HELP! something changed and i cannot replicate this tutorial anymore. i cannot get past the first time you are testing android studio. I remember something like --version react-native@next that is now missing from this tutorial. and now i cannot replicate it at all some one please help.
this is now the 7th time im following the instructions and i get this error first time i try test in the android simulator after trying to create monorepo
Unable to resolve module `./packages/mobile/index
Hi, I updated the tutorial with the latest changes from
react-native@0.59
, search forprojectRoot
to see the updated step involving the filemetro.config.js
.Hey Bruno how are you?
Im still running into a issue. Soon as i complete the web section of the tutorial. my mono repo breaks for mobile. it installs all the modules in packages/mobile/node-modules instead of root/node_modules.
Thus when I run packages/mobile/yarn start i get the following error
"Error: Cannot find module 'E:\2019_PROJECTS\moco-messenger-project\code\repo\test\node_modules\react-native\local-cli\cli.js'"
I dont understand how the web project is affecting the mobile project. Please assist.
kind regards
Make sure you have the correct content inside the root
package.json
and you usedyarn
at the root directory to install the dependencies, not inside each package neithernpm
instead ofyarn
.Compare your code with the repository to see if anything is different: github.com/brunolemos/react-native...
hey bruno,
I just did like you said, I even upgraded react-native to v0.59.0 stable. yet still when i run yarn in the root it still installs packages/mobile/node-modules and not in root/node_modules.
how can i compare an entire project with difference in vs code? are you sure there isnt a step missing? I have tried 3 times now following your exact instructions.
Do you have the latest version of nodejs and yarn? How is your root package.json?
I just checked on my computer, im running yarn 1.130 and node v10.0.0 i believe those are the latest versions.
my root package.json file looks exactly like yours.
{
"name": "myprojectname",
"version": "0.0.1",
"private": true,
"workspaces": {
"packages": [
"packages/*"
]
}
}
like i said im extremely confused becasue i dont understand why the monorepo breaks when i add the web
i just downloaded your project at
github.com/brunolemos/react-native... and when i run yarn in the root it is also installing the packages in the wrong places.
Hey everyone, I downloaded the project, followed the updated tutorial and ran into the same exact issue. I got
/mobile
running again by updating the relative filepath in its package.json to the react-native-cli:good luck!
Hey, you both were right! React Native started getting installed inside
packages/mobile/node_modules
, which is wrong. Changing the path is not the right solution though.I investigated and you need to make sure all your dependencies have the exact same version between the monorepo packages. For example, change your
"react"
dependency inside themobile
andweb
'spackage.json
to the exact same version (e.g.16.8.4
).This will make react-native get installed in the root
node_modules
again and also prevent multiple instances of react being installed in the same project, which would cause all kinds of bugs.Updated the tutorial and also the repository: github.com/brunolemos/react-native...
thank you so much for the new changes in the tutorial. i made the changes to the metro.config.js just like you suggested and i can now run it on android. i will continue with the rest of the tutorial in the morning.
Hi Bruno,
How to use 3rd lib, I want to add
react-native-element
package.It's work well on mobile, but when I run web app, it's error below:
Here's what I added to
config-override.js
onpackages/web
In future, I will add react-navigation and some other packages.
Please help, many thanks!
Did you make it work?
I haven't tried your code but yes, changing
config-override.js
correctly should be enough.Was anyone able to add react-navigation to the project? I got react-native-elements working by following the above comment.
Here's a working example for React-Navigation with web :
github.com/react-native-elements/r...
Thank for documenting all this work thoroughly.
As a React-Native noob, this guide was very easy to follow along ! Cheers!
I got react-navigation's drawer working on the web.
The following is my config-override.js
thanks for the input
i am facing similar issue while using GoogleSigninButton from react-native-google-signin.
i tried adding require.resolve('@babel/plugin-proposal-class-properties') as you suggested below.
also tried to update babel.config.js with the following, didnt worked as well...
module.exports = {
plugins: [
[
'@babel/plugin-proposal-decorators',
{
legacy: true,
},
],
[
'@babel/plugin-proposal-class-properties',
{
loose: true,
},
],
'@babel/plugin-syntax-dynamic-import',
'@babel/plugin-transform-regenerator',
[
'@babel/plugin-transform-runtime',
{
helpers: false,
regenerator: true,
},
],
],
presets: ['@babel/preset-flow', 'module:metro-react-native-babel-preset'],
}
also tried with loose: true
require.resolve('@babel/plugin-proposal-class-properties', {
loose: true,
}),
any suggestion please?
thanks for you time.
Hi Oz, were you able to make it run? Did you try with a compatible release (any before 0.60)?
Hi Nishant,
Unfortunately I was not able to solve it.
I am not sure about earlier versions but I went over many possible suggestions, none of them worked...
Any additional idea that I can try?
Thanks for the reply
Use this config to fix the issue
Add the above code before the
return config
lineThanks Rajendran
i tried adding the above as part of config-overrides.js
it didn't helped.
BTW, i`ve executed 'sudo yarn add @react-native-community/google-signin'
Under the folder 'react-native-web-monorepo/packages/components'
Is that the right location?
Thanks, Oz
An update...
i had to add two more dev dependencies under the web module.
sudo yarn add --dev babel-loader
sudo yarn add --dev @babel/preset-flow
now i am getting different error:
/react-native-web-monorepo/node_modules/@react-native-community/google-signin/src/GoogleSigninButton.js
Attempted import error: 'requireNativeComponent' is not exported from 'react-native'.
With the react-native-version 0.60.4, I have some problems with libhermes.
When starting the android-app, it crashes and I always get the message: E/SoLoader: couldn't find DSO to load: libhermes.so. Any suggestions what to do?
Edit: Fixed my problem. You have to enable hermes, so that it looks like this in your build.gradle in app:
project.ext.react = [
entryFile: "packages/mobile/index.js",
root: "../../../../",
enableHermes: true, // clean and rebuild if changing
]
This works for me thanks!
throws error error: unknown option `--projectRoot'
On modifying
--projectRoot
to
--projectRoots
I get following error
Loading dependency graph...jest-haste-map: Watchman crawl failed. Retrying once with node crawler.
You mean when trying to use jest? If not please check the repository to see what you made differently.
Sorry, I modified my comment. I am getting an error with --projectRoot. I was trying on my own repo. I'll be cloning your repo and then let you know if the problem persists.
I'm using the unreleased react-native 0.59 in this tutorial, some dependencies got a major bump so maybe there are some differences if you are using an older version.
did you find an alternative for --projectRoot please ?
Hi Bruno, thanks for the awesome detailed tutorial with all the updates. And i can see you are very active in comments also which is awesome!
Its really nice to see react's progress towards PWA and cross platform portability.
I am following this tutorial to convert our old add(on Rn 0.59) over to react-native web. I followed this tutorial and setup everything up.
Than i copied the dependencies from our mobile repo's package json to packages/components/package.json. And all other code respectively in src. Idea was to get everything running on ios in this monorepo setup and than go from there to fix whatever needs to be fixed for web.
But i am getting some errors like first i got:
bundling failed: Error: Unable to resolve module
path
from/Users/..../projec/node_modules/react-native-dotenv/index.js
: Modulepath
does not exist in the Haste module mapFixed it by adding
path
explicitly in dev-dependencies. Than started getting error:bundling failed: Error: While trying to resolve module
fs
from file/Users/..../projec/node_modules/babel-plugin-dotenv/index.js
, the package/Users/..../projec/node_modules/fs/package.json
was successfully found. However, this package itself specifies amain
module field that could not be resolved (/Users/..../projec/node_modules/fs/index.js
. Indeed, none of these files existI am not very experienced in react and react-native, i am mainly on Ruby on Rails, so maybe this is something basic which i am getting wrong.
I found: stackoverflow.com/a/39046690/4738391 which suggests omething wrong with node environment or something. But i am a bit confused, i mean i can add these dependencies etc but it should work out of the box for mobile, as it was working fine in previous repo.
Any help will be greatly appreciated. Thanks.
This was a stupid error, i had forgot to add
'module:react-native-dotenv'
in my babel config presets. Adding it solved the problem and i got my mobile app compiled. There were some imports fixes etc in scode needed to be done to run app successfully. But that is done.But now next errors(on web), i posted this SO question: stackoverflow.com/questions/571953...
If i can get any help, would be great.
After cache clean and everything, second error changed to:
error: bundling failed: Error: While trying to resolve module
fs
from file/Users/..../projec/node_modules/babel-plugin-dotenv/index.js
, the package/Users/..../projec/node_modules/fs/package.json
was successfully found. However, this package itself specifies amain
module field that could not be resolved (/Users/..../projec/node_modules/fs/index.js
. Indeed, none of these files existI was running into the same issue and I think I figured out the cause of the problem. I unfortunately don't know how to fix it. It seems to be related to react-native-cli not being able to locate the metro and babel configs because it's being run from the root of the repository.
Since they aren't found, when the bundler comes across react-native-dotenv within a source file it will actually import the source file rather than using the replacement provided by the babel plugin.
Please remove all node_modules folders and .lock files from your project, clear all caches (xcode, —reset-cache, etc), and try following the steps again. Maybe you missed something.
You shouldn’t remove the dependencies from mobile. Check this repo source code and commit diffs: github.com/brunolemos/react-native...
After running yarn to install dependencies, check the contents of packages/mobile/node_modules, and let me know which folders it has. It shouldn’t have any, all should be in the root node_modules.
If nothing works, maybe they released a new version of the cli that broke something, in that case you can use a previous version or investigate the change.
I think i was not clear in my comment, actually it did work with barebone setup as described in this post. It was working perfectly, than i started exporting my mobile code, that's when it exploded.
I wanted to just have mobile app running as it is in this architecture. Than move un-convertable code to mobile and write replacement code for only those in web. But my mobile app is not running with these errors.
I did all the cache clear and everything but same issue. mobile repo's node_modules folder have 2 folder, .bin and metro-react-native-babel-preset.
I am pretty sure it is some of my mobile dependency. I don't know if i should share the package json etc and everything here. It will just get annoyingly long here.
Let me know if i should share that here, or i can reach you through some other medium.
I do understand this blog post is like THE go to blog right now for new projects which want to take this route. but porting old projects is a lot more messy. If i do succeed in porting my app on this architecture and support web, i will leave pointers and can also write an extension of this blog post.
You can run debug session from vscode with React Native Tools extension. But with monorepo its little hacky... btw, read-native run-its --project-path "./packages/mobile/ios" also work
I setup only iOS project with macos: you need to specify "runArguments": ["--project-path", "./packages/mobile/ios"] in debug task in launch.json and symlink with command:
~if somebody already setup android debug, please ping me~
Update:
in vscode task for android add
Thanks for sharing!
I am unable to run android app. When I run
yarn android
I am getting this error.How to update
project.ext.react.cliPath
path?Can anyone suggest what changes did I miss?
I found it. Thank you. I needed to add
cliPath
insideproject.ext.react
in app/build.gradlehow did you do that, could you please add a screenshot or something ?
I apologies for the late response.
Basically what you need to do is go to
mobile/android/app/build.gradle
and update the file as show below.If you want to jump right into the code then you can check out my repo, I have updated the setup.
Nice, thanks for the mention in the article!
Were you able to share code between web and mobile using nohoist? I've had problems with this approach in the past but I don't remember exactly why. It has some downsides but it's definitely much simpler to setup. Everything working well for you this way?
No, sry for taking so long to reply.
Our yarn workspace is just set up to include app / backend / website
And they do not share components.
The problem is that metro does not support symbol links. Which is what yarn workspace uses.
github.com/facebook/metro/issues/1
Hi man, absolutely amazing tutorial, my dev team managed to get it up an running in no time.
However, we are facing two problems, one is related to babel plugins, no matter what i did, i could not add the @babel/plugin-transform-react-jsx, therefore the web package crashes:
The other problem comes with
react-native-vector-icons: ^7.0.0
, we added the:but we could not get them to render in android, instead it shows the crossed box, here is how we implemented it:
try this, please see the following :
step
npm install @babel/plugin-transform-react-jsx or yarn add @babel/plugin-transform-react-jsx
go to config-overrides.js : Then add the package installed to plugins...
please see below,
const fs = require('fs');
const path = require('path');
const webpack = require('webpack');
const appDirectory = fs.realpathSync(process.cwd());
const resolveApp = (relativePath) => path.resolve(appDirectory, relativePath);
// our packages that will now be included in the CRA build step
const appIncludes = [
resolveApp('src'),
resolveApp('../common/src'),
resolveApp('../../node_modules/@react-navigation'),
resolveApp('../../node_modules/react-navigation'),
resolveApp('../../node_modules/react-native-gesture-handler'),
resolveApp('../../node_modules/react-native-screens'),
];
module.exports = function override(config, env){
config.module.rules.push({
test : /.(js|mjs|tsx|ts)$/,
exclude : /@babel(?:\/|\{1,2})runtime/,
use : {
loader : 'babel-loader',
options : {
babelrc : false,
configFile : false,
compact : false,
// The configration for compilation
presets : [
[
'module:metro-react-native-babel-preset',
], // Add this line,
[
require.resolve('babel-preset-react-app/dependencies'),
{ helpers: true },
],
],
cacheDirectory : true,
plugins : [
'@babel/plugin-proposal-class-properties',
'@babel/plugin-proposal-object-rest-spread',
'@babel/plugin-transform-flow-strip-types',
'@babel/plugin-transform-react-jsx'
[
'module-resolver',
{
alias : {
'^react-native$' : 'react-native-web',
},
},
],
],
},
},
});
return config;
};
thanks
paste it to *config-overrides.js
This JS is not valid (check module-resolver)
I am able to run this setup in Dev mode on both ios and android but when I create a releaze, I get an error saying the shared package is not found because it is a symlink and metro bundler doesn't support symlink yet. Were you able to create release builds? If yes, were there any changes required?
ios or android? which react-native version? what's your operating system? try using the latest react-native version and maybe also try using the yarn resolutions feature to force it use the latest version of @react-native-community/cli which as of today is 5.0.1-alpha.0.
So, I have a mobile app in production on iOS and Android that we plan to provide over the browser also. To reuse the logic code in the web app, I am putting those in shared packages managed using the yarn workspaces. Initially, I was using react-native 0.63.3 which uses react 16.13.1 for the mobile app. When I created the web app, it was using react 17.0.1. There were a couple of invalid hook call errors which happen due to conflicting versions of react. I tried no-hoisting react and react-native specific packages but that resulted in some packages under root node modules and some under the package node_modules which becomes problematic during auto-linking. So, I ended up upgrading react-native to 0.64.0-rc.1 which uses react 17.0.1. I was able to build and run the app on both iOS and Android but when I created the release build, I got some errors mainly around not able to find the shared packages since they are symlinks and metro doesn't support symlinks. I read about some workaround by providing extraNodeModules and projectRoots in metro.config.js but I kept on getting one error or the other.
I wanted to ask if you made any special changes to create release builds for your app and what are those?
After banging my head for the last few days to convert the existing app to the mono-repo, I started out creating a brand new app in a mono-repo structure, have a dummy shared package and create release builds from it and then port my code over. I am getting the below error on iOS while creating an archive. I am able to run the app in dev mode:
error: Build input file cannot be found: '/Users/varungupta/Projects/galarm-combined/node_modules/react-native/Libraries/FBReactNativeSpec/FBReactNativeSpec/FBReactNativeSpec-generated.mm' (in target 'FBReactNativeSpec' from project 'Pods')
This error doesn't appear if I create a new project and create an archive without making it into a mono-repo.
One more thing I wanted to ask is do I need to change the paths in node_modules/react-native/scripts/react_native_pods.rb and react.gradle files as they contain some references to node_modules.
See this repository: github.com/brunolemos/react-native...
Dear Bruno,
Thanks for your response. I am already doing #2 as yarn seem to update the packages without updating package.json if there are minor upgrades available. I was not aware of the yarn resolutions but that is pretty cool I do have some node_modules in my web and mobile folder which I will fix using yarn resolutions.
For setting the project root in metro.config.js, I guess I just need to set it to the dirname like projectRoot: path.resolve(dirname, '.')
For the react-native.config.js file, could you please tell me where and how it is used. I created it based on your blog instructions but I am not sure how it is used.
Also, I checked the devhub repository (great resource btw), it doesn't have a react-native.config.js file or setting a projectRoot in metro.config.js file.
Thanks again for your valuable time.
Hi Bruno,
Great tutorial. Thanks for sharing.
Is it possible to maintain the each platform code in 3 different repo. Already I am having iOS and Android project. Now i would like to add some modules in react_native and integrate in iOS and Android. The thing is when the iOS and Android are inside the react-native root repo that is working fine. Linking also happening. But when I separate the iOS folder and when try to install the Pod i am getting issue as "cannot find module react_native/cli" in use_native_modules.
Kindly help me.
Hey, thanks for your awesome tutorial. Just a quick question, how can we host the web version of the app? Cause it seem like it share the file inside the
packages/component/src
folder. I using Docker, is it mean that we need to add all files into the server?The web package has a
build
command, and the result goes to thepackages/web/dist
folder. You should deploy only thedist
folder.I see.. thanks bro.. thats help a lot
Number of issues here:
Changing
jsBundleURLForBundleRoot:@"index"
tojsBundleURLForBundleRoot:@"packages/mobile/index"
leads to resolution errorsLeaving it as it is, leads to
@babel/runtime/helpers/interopRequireDefault does not exist in the Haste module map
. It looks like the packages/mobile/index reference is the way to go but that the react-native command uses packages/mobile as the ref location instead of the project root. From the root I tried two commands (RN 0.59):mobile: start => ../../node_modules/.bin/react-native start --reset-cache
root: start => cd ./packages/mobile && yarn start
root: start => yarn workspace mobile start
In both cases, I get "Unable to resolve module
./packages/mobile/index
from/Users/me/projects/rnw/packages/mobile/.
[...]
`Indeed, none of these files exist:
/Users/me/projects/rnw/packages/mobile/packages/mobile/index
Notice the duplicate packages/mobile reference
Ok, I identified the problem. The
packages/mobile/metro.config.js
is not processed. The projectRoot remainspackages/mobile
instead of ``Hi,
I'm facing the same problem. Did you manage to solve why the
metro.config.js
wasn't processed?Current solution is to set
--projectRoot ../../
at thepackage.json
start script instead ofmetro.config.js
, like it used to be before. Not sure if it's a bug or not.Having some troubles getting react-native-web hoisted and getting everything to run. It insists on staying inside the web/node_modules together with
fbjs
andreact
. I do have some nohoist packages that are linked in the mobile project. I verified all the version numbers. They seem to be alignedTried various cleaning actions. I'm getting errors from hoisted elements not being able to find
react-native-web
shouldn'tmetro-react-native-babel-preset
be implemented as a preset in the web project? This seems to be indicated inreact-native-web
examplesYou need to use the exact same version of react between all package.json that have it.
Thanks Bruno for this awesome post!! It was the only way I was able to make react-native work within a monorepo structure.
Currently I am working on a web + ios + tvos app and I was wondering how should I configure the tvos app since it react-native relies on npm:react-native-tvos@0.62.2-0. Following your tutorial I have:
1) Added 'react-native' and 'react-native/**' into tv package.json noHoist in order to get the react-native stored within the tv package and not use the ios hoisted one.
2) Use your metro.config.js configuration but also added the extraNodeModules in order to override the react-native alias to use the tv package one:
3) Change the AppDelegate.m file with your configuration
4) Within the
Bundle React Native code and Images
section I have added theEXTRA_PACKAGER_ARGS
but kept the react-native path as it is in order to use the tv package one:Is this okay? Am I missing something else?
Thank you very much, your post is awesome <3!
Sir, will
createBrowserApp(switchNavigator, { history: 'browser' })
work when I host a React Native Web app on GitHub Pages to have such these URLs:Is it possible on GitHub Pages with React Native Web?
Hi,
I downloaded the repo, I am able to run the web app, but I have issue with ios
Can you tell me why I am getting the below issue, I am trying to do pod install inside my ios folder
aal003110384:ios reerer89989823$ pod install
[!] Invalid
Podfile
file: cannot load such file -- /Users/reerer89989823/Desktop/codebase/Shipment/react-native-web-monorepo-master/packages/mobile/node_modules/@react-native-community/cli-platform-ios/native_modules.# from /Users/reerer89989823/Desktop/codebase/Shipment/react-native-web-monorepo-master/packages/mobile/ios/Podfile:2
# -------------------------------------------
# platform :ios, '9.0'
Thanks a lot for this great tutorial.
i`ve probably missing it in the comments but i am not sure what pakage.json i should use for adding additional dependencies.
should i only update the root /package.json
or individually also
in addition, only yarn should be used?
Thanks again.
Yes only yarn because npm doesn’t have the “workspaces” feature.
You’ll add the dependencies in the package that’s using them. Most of it will probably be in the components. Don’t add at the root unless it’s things like lint etc.
Hello,
Thank you so much for the article.
I have pulled down your repo, installed the deps and ran it. Metro bundler runs well but I do not see anything on the emulators.
Is there a step that I am missing?
Thanks
Siya
To run the mobile apps you can open Xcode/Android Studio and press the Run button.
Yeah, I realised that is how you were running it. I am a fan of the terminal, so I prefer that way. But for some reason I thought that running the bundler would mount the project into the emulator. Thank you so much for the article, I found it very helpful.
How did you configure auto reloading?
Thanks
It's possible to have native apps instead of Electron, with the same code. We can use react-native-windows and react-native-macos, for example. In a few days I'll experiment with Marzipan, a secret official Apple project to run iOS apps on the mac.
Thanks for the post! I have been able to get it going without any problems using the outlined steps.
I am facing problems however when i try to install 3rd party dependencies after getting the initial setup going. It will be great if the post could be updated with steps on how to install dependencies post setup.
For example i wanted to add
react-native-gesture-handler
and a few other native libraries but somehow i keep getting some issue or the other. I triedyarn workspace components add react-native-gesture-handler
and it installs but i am unable to use it since i get IDE errors about some constants not being found. Similar issues with other libraries as well. Auto-linking somehow doesn't seem to reflect.Should
jettifier
be installed and added in thepost-install
ofpackage.json
(and if so whichpackage.json
- root or the ones inside the packages) if running commands likeyarn workspace mobile start
? Basically some tips on how to add 3rd party libraries for both mobile and web will really help. Thanks again for the post - its my first attempt at a monorepo and this post really helped iron out all my initial doubts but i am also obviously making some newbie/rookie mistakes so there are some stumbling blocks to really get started with it.Ok. so with a few more attempts i got it going for a critical dependency in my packages. I am using the versatile
Navigation
router (github.com/grahammendick/navigation) for all the routing requirements in all my packages, across all the router's supported platforms (which is basically everything web, mobile, native etc).After following all the steps in the post i initially installed the router into my equivalent of the
components
package. I got it to work for the web but, for thereact-native
implementation, the navigation router has native code forAndroid
andIoS
and somehow it wasn't getting auto-linked. The solution was to simply install it to my equivalent of themobile
package and everything seems good! im still not sure of thejettifer
post-install step but it should not be an issue i think.I recently worked on a project which used
monorepo
. It was my first introduction to it. I must say, I was not impressed. But hear me out. I've worked in the mobile app industry for over 10 years. I've seen architectures come and go.Conceptually, I understand the use case for implementing such a design. In practice, I found it unintuitive, incredibly confusing, and slowed my project onboarding process to a crawl. I had six weeks to perform the job for which I was hired. The first week was spent wrapping my head around how to simply walk through the workflows. That cost the client a lot of money and reduced my available billable time by 20%. Imagine what would happen if the client had to onboard a less-experienced dev.
To be fair, there are a lot of unknowns here. It was my first hands-on with the design. Maybe the client's architect built it incorrectly? Would my experience have improved had the repo been better documented, i.e. had any doc at all? I don't really know.
I'm off that project and on my way to others. I will withhold judgment for now because this project could've been a fluke. I just wanted to interject my experience. For any non-trivial project, it's important for readers to remember that the business drives technology decisions. If a monorepo design pattern solves the particular business requirement, then I hope this tutorial helps you jumpstart your project.
Hello Bruno, awesome tutorial as I mentioned!
I have another question though, and it's related to builds. I've set the build phase as Release (so I can run it without a server) but I get an error in Xcode:
File /Users/--/Library/Developer/Xcode/DerivedData/project-name/Build/Products/Release-iphoneos/project-name.app/main.jsbundle does not exist. This must be a bug with'
Is there anything special I need to set here to get Xcode to build it?
No, this error is unrelated to this tutorial. Try cleaning the caches, e.g. Product > Clean and yarn start --reset-cache.
Thank you cheers! I had thought it was something related to the workspace edits. Turns out there's an issue in react-native about github.com/facebook/react-native/i....
So how did you fix it? I have the same issue. The GitHub issue link you provided is not working for me :-/
I want to discuss something we are facing after getting a great kickstart from this post. That is responsiveness problem.
We have made significant progress in our journey of converting existing RN apps to RNW. In-fact i am now writing a series of articles on my experience, first one was written last weekend and i plan on writing next this weekend. Here is the first part:
medium.com/@ziaulrehman/part-1-con...
For my use case, i am using a similar approach to this library: github.com/calinortan/react-getscreen it basically adds event handlers which trigger a state change hence re-render whenever screen size crosses one of pre-defined(configurable) widths.
This allows us to add conditional styles etc on some elements, or even hide/show some elements conditionally. This along with Platform.OS === 'web' is basically all we are doing for now. It doesn't look very clean but it works and gives us responsive design.
What techniques have you used(if any) for this? What worked best? And what would you suggest. Thanks.
I have a custom hook that checks the app dimensions and returns if it’s small/medium/large: github.com/devhubapp/devhub/blob/m...
Besides that, make sure to always use flexbox and percentages where makes sense, not fixed widths in pixels.
Yeah similar approach with HOC for class components. I actually just released a small RN component for this(actually my first npm package). npmjs.com/package/react-native-get...
Olá bruno, baixei a versão mais recente do monorepo, mas está dando esse erro,
sabe dizer porque? e como resolver?
Loading dependency graph, done.
[1] error: bundling failed: Error: Unable to resolve module
../../../commons/reducers/
fromE:\sistemasagv\appwmsmobile\app-rf\packages\components\src\screen-mobile\Reducer\index.js
: The module../../../commons/reducers/
could not be found fromE:\sistemasagv\a
. Indeed, none of these files exist:ppwmsmobile\app-rf\packages\components\src\screen-mobile\Reducer\index.js
[1] *
E:\sistemasagv\appwmsmobile\app-rf\packages\components\commons\reducers(.native||.android.js|.native.js|.js|.android.json|.native.json|.json|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx)
[1] *
E:\sistemasagv\appwmsmobile\app-rf\packages\components\commons\reducers\index(.native||.android.js|.native.js|.js|.android.json|.native.json|.json|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx)
[1] at ModuleResolver.resolveDependency (E:\sistemasagv\appwmsmobile\app-rf\node_modules\metro\src\node-haste\DependencyGraph\ModuleResolution.js:163:15)
[1] at ResolutionRequest.resolveDependency (E:\sistemasagv\appwmsmobile\app-rf\node_modules\metro\src\node-haste\DependencyGraph\ResolutionRequest.js:52:18)
[1] at DependencyGraph.resolveDependency (E:\sistemasagv\appwmsmobile\app-rf\node_modules\metro\src\node-haste\DependencyGraph.js:283:16)
[1] at Object.resolve (E:\sistemasagv\appwmsmobile\app-rf\node_modules\metro\src\lib\transformHelpers.js:261:42)
[1] at dependencies.map.result (E:\sistemasagv\appwmsmobile\app-rf\node_modules\metro\src\DeltaBundler\traverseDependencies.js:399:31)
[1] at Array.map ()
[1] at resolveDependencies (E:\sistemasagv\appwmsmobile\app-rf\node_modules\metro\src\DeltaBundler\traverseDependencies.js:396:18)
[1] at E:\sistemasagv\appwmsmobile\app-rf\node_modules\metro\src\DeltaBundler\traverseDependencies.js:269:33
[1] at Generator.next ()
at asyncGeneratorStep (E:\sistemasagv\appwmsmobile\app-rf\node_modules\metro\src\DeltaBundler\traverseDependencies.js:87:24)
Thanks, Bruno! I really needed this. Can this be applied to Lerna instead of Yarn Workspaces?
I've never used Lerna because Yarn Workspaces does all I need, so I don't know the answer. But let us know! 🙌
Hello Bruno,
1.) Now that Expo for Web was released, what would you recommend?
To setup a monorepo for Mobile and Web packages or to use Expo for Web?
There's also a React Native Web CLI called
create-react-native-web-app
.2.) And also could you give some guide how to setup Next.js as a package of this monorepo? Upon running
yarn create next-app --example with-react-native-web next
, here's the response:Thanks for any response!
Expo has it's own set of limitations, e.g. I've been using hooks in production since november 2018 and it only arrived in expo may 2019. If you are ok using expo go for it! They seem to be doing great progress.
About next, I don't know what the issue is, try creating the project separately and copy pasting the folder into the monorepo. Otherwise open an issue with them about monorepo support.
When I use "yarn start", my web app works pretty much exactly the way it should. If I use "yarn build", and then "serve -s build", however, it gives me an error for createBrowserApp:
AppNavigator.ts:99 is:
react-navigation, @react-navigation, react-native-gesture-handler, react-native-reanimated, react-native-screens, react-navigation-stack, and @react-navigation/web are all in my main project's package.json, and "brought over" via
I can't figure out why it works via "yarn start", but not on "yarn build". I've spent a while searching, and I can't seem to find an answer. Anyone have any ideas?
when you say, "You can now run the Android app! 💙" what does that mean? what steps do i take to run the app?
There is a "Run" green button inside Android Studio that will open the app inside an emulator.
Thank you so much. I just wanted to make sure that im not missing anything because I couldn't get "react-native run-android" to work.
You have several options to make
react-native
commands work.node_modules
directory of your package make a symlink to thenode_modules/react-native
directory from the root of the project because react-native-cli need to findcli.js
innode_modules/react-native
.yarn react-native run-android
"run-android": "react-native run-android"
and then useyarn run-android
I used second option. However, I am using third party libraries and it cannot resolve these dependencies!
Solved,
I changed projectDir attribute of library in android/settings.gradle
Bruno, this is a great tutorial. I have been able to successfully migrate an existing react native project to the mono repository and can run and compile the mobile app for android. However, I am having issues building the web portion of the project. Any tips on how to debug? I am getting the project to start to compile but get in error Attempted import error: 'initStore' is not exported from '../store'. I have altered the index.js for web to pull in the components package.
Thanks
Nice article, I have a bit similar example for code sharing between react-native and next.js with react-native-web here: infinidraw.live/
Source: github.com/msand/InfiniDraw
Expo version: github.com/msand/InfiniDrawExpo
But, it's not really clean architecturally, it's a single project rather than separate folders & package.json for web and native, causing some tradeoffs. Been thinking should slit it up a bit similar to this.
HELP!!!!!!
I have implemented react-navigation-drawer in my project. Just using two menu items there
( HomeScreen & ProfileScreen )
react-navigation-drawer issue link
The drawer not closing automatically on the browser ... but on mobile it works fine.
Can you please give me any reference where i can separate the style of web and mobile?
Is it possible to incrementally add rnw to an existing project?
The current project set up is a 3 package monorepo with redux being the only code that is shared. The goal is to share more code so I’m thinking to create a 4th package that only contains react native web components and is imported by the react and react native packages. Do you see any issues with this approach or pieces not working?
Hey, I'm looking at your articles(very comprehensive), among others, and trying to understand the need to add CRA to the stack, here. Would using
react-native
andreact-native-web
not do the trick?Also, changing the
import
statements in node_modules means you check in your node_modules? Or you make the edits every time you yarn install? (or, i missed smth with yarn workspaces 🤔?)Thanks for your time and for the article!
CRA makes things easier, but it is not required.
It doesn't make any change inside node_modules, only on normal project files.
Hey bro,thanks for the awesome tutorial..But I cant run the project in a real android device.
warn Failed to connect to development server using "adb reverse": spawnSync adb ENOENT
info Starting the app...
have do u any idea??
Hi, after going through the steps listed under "ios changes" above, when i run "yarn workspace mobile start" it does launch the app in the simulator but i get a red screen with error (attaching image). Please help
root package.json should add "," to end of "workspaces":{...}
like this:
{
"name": "myprojectname",
"private": true,
"workspaces": {
"packages": [
"packages/*"
],
"nohoist": []
},
"dependencies": {
"react-native": "0.61.3"
}
}
Hi.
If I understand correctly, the React native and react dependencies should be installed in the root node_modules to avoid having multiples version of the packages in the app.
Does this apply to every dependencies the packages share? (ex. react-native-elements)
Thanks for the guide!
Yes, all dependencies should be installed at the root node_modules, this way it's less likely you'll face problems. Please note that this doesn't mean putting it in the root package.json -- you put the dependencies at all the packages/xxx/package.json that use this dependency.
Hi, thanks for this tutorial, monorepo is amazing, however how could rename project "myprojectname" to "mycustomname" properly?
Other hand side, for windows to start android studio, add enviroment var to PATH var list:
...
C:\Program Files\Android\Android Studio\bin
...
and replace:
Thanks
Hi! I followed the tutorial and everything works as expected, but I'm having trouble when adding NativeBase, as it makes me add the babel plugin @babel/plugin-proposal-class-properties, which I haven't had success adding since the yarn workspace pattern still confuses me a little bit.
Where should I install the dev dependency? And how should I enable the plugin? In config-overrides.js?
Thank you so much!
Is the
--projectRoot
flag a metro config option? trying to set this up with the 0.59.0 release and running into haste map resolution issues for@babel/runtime
Please refresh the page, I've updated the tutorial a few minutes ago with the new step related to
projectRoot
. You don't set--projectRoot
anymore, you add it tometro.config.js
.ha - thank you. i had just made and tested the same update and got it working. cheers!
I tried to set up monorepo 2-3 months ago. Faced lots of errors with React Native. I have been searching for any decent material. I always suspected issue to be of old articles as the dependencies have changed. So the steps stated in the article or video were always not working for me.
I faced same stuff with your article. Tried it almost a year after you writing it.
I faced stackoverflow.com/questions/473994... issues everytime I tried to run the mobile app. This particular issue was extremely nasty github.com/facebook/react-native/i.... I never hated another library so much as React Native because of all this shit. Or some other module not popping up. I managed to get mobile app work only just once. But I couldn't get it to work when I imported the app from Components package. So I felt somehow my monorepo was not at all working. Almost all the stack overflow or github issues stated only few repeated solutions like clear cache, delete node_modules are re run it. In my scenario situation seemed to be deteoriating. Atleast mobile app was connecting to simulator, then after trying out these solutions it started giving the error of Metro server of not being up and running. Which didn't make sense simulator was able to make call metro server.
Then I thought atleast web would work but it didn't either it always gave me this error -
Module not found: Can't resolve 'react-native/Libraries/NewAppScreen. Because App.js in components package was using this library in updated version of React-Native CLI. I followed all the steps diligently I even found some more steps were required as well.
I don't know how you managed to get it to work. I really envy that but I have wasted too much time to just get the infrastructure setup managed to do total zero work. I worry if I try to integrate other libraries I'll face the same issues.
Just remove all the dummy components from
react-native/Libraries/NewAppScreen.
on the app.js screen. It will then workHey Bruno, don't know if you can't help me but I've followed the post to setup the monorepo. Almost everything worked, web is working well, but I'm having trouble with the React Native part.
I think the problem is that some dependencies are being installed on the workspace node_modules instead of the project's root node_modules so some dependencias are in the root and others are inside the project - almost as if it was set to no-hoist these dependencies (it was not).
Do you any clue why this is happening?
Thanks for writing the post
Olá Bruno, segui seu tutorial e tudo esta funcionado tirando a parte do android, no ios eu consigo rodar o projeto normalmente, ja instalei vários pacotes, e continua funcionando. no android estou rodando o comando react-native run-android e ele não copila, da sempre um erro diferente, peguei o seu repositório e comparei, acho que fiz todo o passo a passo e não esta dando certo, voce pode me dar uma ajuda?
Amazing work, congratulations!
Did you consider using react-navigation for web and native or react-router just for web?
What made you skip those options?
Thank you
React Navigation v3 wasn't available yet, v2 didn't work very well on web. I may use it on next projects if it is better now.
I'm gonna check v3, thank you!
Hey Bruno,
Thank you mate for this awesome article to solve monorepo solution.
I also created a repo for sharing code between two react native apps using JavaScript support only. May be it will be helpful for others who is looking for same case like mine.
ReactNativeMonorepoApps
hi Bruno , hope you're doing well
I just tried to set up this tutorial step by step but after I added >>config-overrides.js file in packages/web and run yarn start inside packages/web I got this error:
yarn run v1.17.3
$ react-app-rewired start
Cannot read property 'oneOf' of undefined
error Command failed with exit code 1.
Hey Bruno how are you?
I am facing issue
src/index.tsx:3:21 - error TS2307: Cannot find module 'components/src/App'.
import { App } from 'components/src/App'
I have downloaded your code as it is and even tried the above process multiple time but still facing the same issue.
My package manager responds to
http://localhost:8081/packages/mobile/index.js
but gives a 404 onhttp://localhost:8081/packages/mobile/index
. Any ideas?I also see this error Error: Unable to resolve module
./packages/mobile/index
from/Users/boo/project/packages/mobile/.
: The module./packages/mobile/index
could not be found from/Users/boo/project/packages/mobile/.
There seems to be a path set wrong. But it all looks ok at the code level
Thank you for the great article, Bruno!
Hi, first of all: thank you, thank you so much for the guide, it was the only way, for me, to correctly setup my project.
Then, i've two suggestions:
1) The guide talks about sharing the "components", so i was fearing i had to reqrite all the routing, sagas, redux, etc, etc part for web. Instead i found the project share the whole app, so maybe "components" is not the perfect word here?
2) I had to add some components that, to work on web, required to use another custom web component and add the alias to webpack (es: react-native-linear-gradient/react-native-web-linear-gradient).
I used customize-cra (github.com/arackaf/customize-cra) but it took me some time to port the actual webpack's override i had (from this guide) to the customize-cra one with the alias. Don't you think it would be nice to use customize-cra on this guide? Or maybe just explain a way to create the alias without the customize-cra module?
Thank you for the great job.
HELP! this is the 6th time i have tried to replicate this tutorial, but something changed and i cannot replicate this tutorial anymore. i cannot get past the first time you are testing android studio. I remember something like --version react-native@next that is now missing from this tutorial. and now i cannot replicate it at all some one please help.
the error reads "Unable to resolve module
./packages/mobile/index
"how many changes occurrences of node_modules/react-native/ are you replacing with ../../node_modules/react-native/ vscode picks up 26 places where i change it. is this correct?
i cannot get it to work please assist.
Error: Unable to resolve module
./packages/mobile/index
fromE:\2019_PROJECTS\moco-messenger-project\code\repo\wepay4\packages\mobile/.
: The module./packages/mobile/index
could not be found fromE:\2019_PROJECTS\moco-messenger-project\code\repo\wepay4\packages\mobile/.
.Nice article, thanks!
Any more info on „If you use expo, there may be some news coming to you soon“? 😇
I believe they are about to release built-in web support soon, but I have no insider information, it's just an impression I had after seeing some tweets from their team members. Also some of their dependencies recently got web support merged. You can ask them for more details: twitter.com/Baconbrix/status/10983...
Awesome, I think you might be on to something here 🧐
He's right 👉 expoweb.netlify.com/
Is it any chance this will work with RN 0.6? There is a problem with --projectRoot param, as 0.6 doesn't recognize this parameter. Checked your GH repository, looks like you are still working with 0.59
I've believe that the monorepo was installed correctly react-native was installed in the root directory. What confused me was this step "Open your favorite editor and use the Search & Replace feature to replace all occurrences of node_modules/react-native/ with ../../node_modules/react-native/". I'm over here like what occurrences? Where exactly are the occurrences? In what files? Any help, please and thank you.
Hi Bruno, In the DevHub app for the web or windows, how did you get multiple screens to fit in parallel (Side by Side) ? Can you point me in the correct direction.
I tried reading the code but could not understand it as I have no idea how electron works with React-Native. Anything would help.
Also, a bigg thank you for updating this tutorial.
If you are talking about the columns, they aren't "multiple screens" in parallel, it's just an horizontal FlatList component. About modals and other navigation components, I wrote from scratch, since I'm not using any navigation library (I may use some soon).
Hi, I have a question that I cannot grasp , where is the rule that allows to have different environments files? .web , .android , .ios, .native , etc and being selected auto-magically ?
This is handled by react-native and create-react-app.
oh sorry, My bad !
I've just found it :
facebook.github.io/react-native/do...
thanks for the quick response :)
Thanks
I wonder how you rate the viability of using RNW in production with RN0.59, given that the current supported version is 0.55 with 0.57 under construction
I've been using it on DevHub with no problem. What it means is that the javascript code running on the web is the one from rn-0.55, not rn-0.57. This will start to become an issue when react-native remove some components, for example, so react-native-web will need to stay up to date.
Thanks Bruno , Can we use react-native run-ios or android at packages/mobile folder ? I want to start the simulator .
Yes you can, check the readme: github.com/brunolemos/react-native...
This is fantastic! Now let's see it with Next.js!
Next.js has an official example using react-native-web, check it out: github.com/zeit/next.js/tree/maste...
Can we achieve this by using npm?
No, Workspaces is a feature available on Yarn but not on the npm cli, afaik.
I have a problem with build android: Text must not be null or empty in react native 0.60 build error
Great article, thanks! Really helped me to transform existing project to monorepo. Except one babel plugin. Any thoughts on how to get babel-plugin-module-resolver working with your structure?
it's a shame that I can't see devhub source code again. seems what i can see only is the landing page code ..
Hi Bruno, does this repo also works for microservices architecture?
Thanks! 🙌
I couldn't get this worked in 3 days so I'm gonna post some of my stuck here.
I wish there was a recent version of this repo.
react-native gets installed on packages/mobile/node_modules, even though monorepo dependencies share the same version.
I could not reproduce. Check the versions of all packages again, remove the "" from the start of them. You can fork this instead: github.com/brunolemos/react-native...
Any chance of an update with integrating Storybook?
No but it should just work if you follow their react native guide: storybook.js.org/docs/guides/guide...
medium.com/@agent_hunt/storybook-s...