Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

monorepo: expo with yarn workspace and using expo install

Tags:

expo

I'm trying to setup a React and Expo monorepo project, everything seems good until I want to install react-navigation with expo install according to the react-navigation docs, because expo install use yarn in the background and because it's a workspace environment this error pop out, which I have no idea how to bypass. any ideas?

yarn add v1.21.1
info Visit https://yarnpkg.com/en/docs/cli/add for documentation about this command.
error Running this command will add the dependency to the workspace root rather than the workspace itself, which might not be what you 
want - if you really meant it, make it explicit by running this command again with the -W flag (or --ignore-workspace-root-check).     
yarnpkg exited with non-zero code: 1
Set EXPO_DEBUG=true in your env to view the stack trace.
like image 203
e4gle Avatar asked Jan 26 '20 15:01

e4gle


People also ask

What is Monorepo Expo?

Monorepos, or "monolithic repositories", are single repositories containing multiple apps or packages. It can help speed up development for larger projects, makes it easier to share code, and act as a single source of truth. This guide will set up a simple monorepo with an Expo project.

Who uses Monorepos?

This practice dates back to at least the early 2000s, when it was commonly called a shared codebase. Google, Meta, Microsoft, Uber, Airbnb, and Twitter all employ very large monorepos with varying strategies to scale build systems and version control software with a large volume of code and daily changes.

What are yarn workspaces?

Yarn Workspaces is a feature that allows users to install dependencies from multiple package. json files in subfolders of a single root package. json file, all in one go. (…) Yarn can also create symlinks between Workspaces that depend on each other, and will ensure the consistency and correctness of all directories.

What is Nohoist?

“nohoist” enables workspaces to consume 3rd-party libraries not yet compatible with its hoisting scheme. The idea is to disable the selected modules from being hoisted to the project root. They were placed in the actual (child) project instead, just like in a standalone, non-workspaces, project.


1 Answers

This is how i got it to work with expo-yarn-workspaces.
In my global package.json file i added the following codes.

{
  "private": true,
  "workspaces": [
    "packages/*"
  ],
}

then i created a folder in my root directory called packages.
In my terminal i cd packages then i created a new expo project with this command

expo init app

Define the name and version properties in package.json.

then i installed all my dependencies:

npm install

or

yarn

now its time to install expo-yarn-workspaces with

npm install --save-dev expo-yarn-workspaces

or

yarn add expo-yarn-workspaces -D

After that you add this script in your package.json

"scripts": {
    ...,
    "postinstall": "expo-yarn-workspaces postinstall"
  },

create a file and name it metro.config.js and paste the following code

const { createMetroConfiguration } = require("expo-yarn-workspaces");

module.exports = createMetroConfiguration(__dirname);

in your package.json replace/add this line of code

"main": "__generated__/AppEntry.js",

Run:

npm run postinstall

or

yarn postinstall

then you can start your app with the following command to clear the cache:

npm start --clear

or

yarn start --clear

NOTE: If you're running create-react-app and react-native they most be of the same version so you have to run npm install react react-dom in both folders to be able to use the same version.


My github repo

Useful links:

Article https://divinehycenth.com/blog/yarn-workspace-monorepo.

Github https://github.com/expo/expo/tree/master/packages/expo-yarn-workspaces

Hope it solves your problem.

like image 55
Divine Hycenth Avatar answered Oct 21 '22 18:10

Divine Hycenth