Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify setup for monorepos #877

Closed
2 tasks
sibelius opened this issue Dec 4, 2019 · 19 comments
Closed
2 tasks

Simplify setup for monorepos #877

sibelius opened this issue Dec 4, 2019 · 19 comments
Assignees
Milestone

Comments

@sibelius
Copy link
Member

sibelius commented Dec 4, 2019

Describe the Feature

On iOS, you need to set PROJECT_ROOT in order to make CLI work in a monorepo. On Android, same set of configuration is required.

Possible Implementations

  • Get rid of PROJECT_ROOT on iOS because it's not needed anymore
  • Figure out strategy for Android.

Related Issues

related to #865 #656

@sibelius
Copy link
Member Author

sibelius commented Dec 4, 2019

this is my error when building

Error: Cannot find module '/app/packages/app/node_modules/react-native/cli.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:794:15)
    at Function.Module._load (internal/modules/cjs/loader.js:687:27)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)
    at internal/main/run_main_module.js:17:11 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

as it searches for react-native in the wron glocation

@sibelius
Copy link
Member Author

sibelius commented Dec 9, 2019

I've got this working like this

project.ext.react = [
    entryFile: "index.js",
    bundleInStaging: true,
    devDisabledInStaging: true,
    bundleInAlpha: true,
    devDisabledInAlpha: true,
    enableHermes: false,  // clean and rebuild if changing
+    root: "../../../../"
]

modifying root path on app/build.gradle

but this raised another issue

error Invalid platform "android" selected.
info Available platforms are: "native". If you are trying to bundle for an out-of-tree platform, it may not be installed.

it only shows the android command inside react-native packages inside our monorepo (like here https://github.com/entria/entria-fullstack/tree/master/packages/app), but fails on root

what is the proper solution for this?

@sibelius
Copy link
Member Author

fixed android like this

project.ext.react = [
   cliPath: "../../node_modules/react-native/cli.js",
]

@sibelius
Copy link
Member Author

ios also need this

export NODE_BINARY=node
export PROJECT_ROOT="$PROJECT_DIR/.."

../../../node_modules/react-native/scripts/react-native-xcode.sh

@grabbou
Copy link
Member

grabbou commented Jan 13, 2020

Yeah, in my experience, this is the major root cause for all of the monorepo issues that a lot of people are facing these days.

Any idea how we could plug into it? Setting dynamically variables?

@sibelius
Copy link
Member Author

this is a hard one, maybe a monorepo documentation guide could be good enough

@yuzhakovvv
Copy link

fixed android like this

project.ext.react = [
   cliPath: "../../node_modules/react-native/cli.js",
]

In my case it was

project.ext.react = [
    ...
    entryFile: "index.js",
    cliPath: "../node_modules/react-native/cli.js",
]

Because project structure is:

node_modules/
- react-native/
mobile/
- node_modules/
- index.js
shared/
web/

@grabbou
Copy link
Member

grabbou commented Mar 19, 2020

@sibelius, following up to my discussions #656 (comment), we should be able to safely remove PROJECT_ROOT.

In theory, we always want it to be ios/.. and even if it ends up being deeper in the hierarchy, CLI knows what to do.

@grabbou
Copy link
Member

grabbou commented Mar 19, 2020

If you look at the details of the commit that introduced this change - facebook/react-native@9ccde37, I believe its assumptions are no longer true and we can get rid of it.

Waiting for Janic to get back to me whether this is right and I believe we can follow up with a PR. This particular change shouldn't require anything on the CLI side.

@grabbou
Copy link
Member

grabbou commented Mar 19, 2020

The ideal for me is this on iOS, regardless of your step

export NODE_BINARY=node
../../../node_modules/react-native/scripts/react-native-xcode.sh

@grabbou grabbou changed the title handle CLI_PATH on monorepo features Simplify setup for monorepos Mar 19, 2020
@grabbou grabbou pinned this issue Mar 19, 2020
@grabbou grabbou self-assigned this Mar 19, 2020
@grabbou
Copy link
Member

grabbou commented Mar 19, 2020

As per Janic's comment, we're good to go with the suggested changes.

I will proceed with them soon and link a PR that is landing in React Native as a result.

@grabbou grabbou added this to the 5.x milestone Mar 19, 2020
@grabbou
Copy link
Member

grabbou commented Mar 20, 2020

Just filled in React Native PR - facebook/react-native#28354. Appreciate your review and support in additional explanations :)

@tad3j
Copy link
Contributor

tad3j commented Jun 2, 2020

Did anyone got build working with hermes enabled in a monorepo?

I've got hermes to work by telling react where it's files are:
hermesCommand: "/mnt/c/Users/user/WebstormProjects/project/node_modules/hermes-engine/%OS-BIN%/hermes",

This solved the initial issue of not being able to build becasue hermes executable was not found, however with this fix in place build is still failing with:

internal/modules/cjs/loader.js:638
    throw err;
    ^

Error: Cannot find module '/mnt/c/Users/user/WebstormProjects/project/packages/mobile/node_modules/react-native/scripts/compose-source-maps.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)

> Task :app:bundleReleaseJsAndAssets FAILED

...because it is looking for node_modules folder in wrong location; the correct location is the same as in hermesCommand, /mnt/c/Users/user/WebstormProjects/project/node_modules.

Building works with enableHermes: false, so it only fails with hermes is enabled.

Any suggestion how to get around this?

My full react config from gradle:

project.ext.react = [
  entryFile   : "index.js",
  enableHermes: true,  // clean and rebuild if changing
  hermesCommand: "/mnt/c/Users/user/WebstormProjects/project/node_modules/hermes-engine/%OS-BIN%/hermes",
  cliPath     : "../../node_modules/react-native/cli.js",
]

EDIT:
I figured out that one can also use relative path for hermesCommand, which is a bit different than cliPath. Seems like one has to point hermesCommand 2 levels higher in directory structure (when compared to cliPath):

hermesCommand: "../../../../node_modules/hermes-engine/%OS-BIN%/hermes",

EDIT2 / SOLUTION:
By digging through the source code I found that composeSourceMapsPath config key exists, so my whole react config looks like this:

project.ext.react = [
  entryFile   : "index.js",
  enableHermes: true,  // clean and rebuild if changing
  hermesCommand: "../../../../node_modules/hermes-engine/%OS-BIN%/hermes",
  composeSourceMapsPath: "../../node_modules/react-native/scripts/compose-source-maps.js",
  cliPath     : "../../node_modules/react-native/cli.js",
]

This got me pass the issue with compose-source-maps.js and I think files were successfully processed by hermes. However, there seems to still be an issue with code-push, since build fails with

Error: Cannot find module '/mnt/c/Users/user/WebstormProjects/project/packages/mobile/node_modules"/react-native-code-push/scripts/generateBundledResourcesHash.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)

...so this time code-push is looking node_modules in wrong location. I think this issue is out of scope here, so I guess I should check Microsoft's github regarding this.

@CelsoTeixeira
Copy link

CelsoTeixeira commented May 13, 2021

Following @tad3j answer, using react-native 0.63.2 I had to change hermesCommand.

hermesCommand: "../../../../node_modules/hermes-engine/%OS-BIN%/hermes",

Changed to:

hermesCommand: "../../../../node_modules/hermes-engine/%OS-BIN%/hermesc",

I still need to pass all other configuration, composeSourceMapsPath, cliPath and hermesCommand. Not passing composeSourceMapsPath was throwing a error.

My monorepo configuration is more or less like this, trying to not use nohoist at all.

monorepo
  - node_modules
     - react-native
  - packages
      - app
      - web
      - any other shared package

My final build.gradle project.ext.react is the following:

project.ext.react = [
    entryFile: "packages/app/index.js",
    enableHermes: true,  // clean and rebuild if changing,
    hermesCommand: "../../../../node_modules/hermes-engine/%OS-BIN%/hermesc",
    composeSourceMapsPath: "../../node_modules/react-native/scripts/compose-source-maps.js",
    cliPath: "../../node_modules/react-native/cli.js",
]

@eliw00d
Copy link

eliw00d commented Sep 8, 2021

Has anyone that uses monorepos been able to get Sentry to work? I posted there but figured I'd ask here as well. I followed the examples in here for project.ext.react and it has solved everything except Sentry source maps.

@liamjones
Copy link
Contributor

@eliw00d Yes, we do, I'll post on the Sentry ticket since it's specific to that.

@kelset kelset unpinned this issue Jan 28, 2022
@xzilja
Copy link
Member

xzilja commented Jan 29, 2022

Had issues with ios not finding index.js entry with solutions above, what ended up working is

export PROJECT_ROOT=$PROJECT_DIR/..
export ENTRY_FILE=$PROJECT_ROOT/index.js

@phvictorino
Copy link

Had issues with ios not finding index.js entry with solutions above, what ended up working is

export PROJECT_ROOT=$PROJECT_DIR/..
export ENTRY_FILE=$PROJECT_ROOT/index.js

after days trying to fix the problem finally this worked for me

@sibelius
Copy link
Member Author

good enough for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants