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

How to solve "Cannot find module 'react-native/cli" correctly? #2289

Open
AlexanderEggers opened this issue Feb 9, 2024 · 4 comments
Open
Labels
question Further information is requested

Comments

@AlexanderEggers
Copy link

AlexanderEggers commented Feb 9, 2024

Ask your Question

Edit: I just noticed #1600 already discusses the issue. I must have missed that when creating this issue. Feel free to close my issue, I will keep that open in case that helps solve the underlying issue.

I have the following use case in my project setup:

Screenshot 2024-02-09 at 12 28 59 pm

Essentially I want to use react-native (0.73) in an existing Android project without having to actually update its structure or anything related to that. Currently the react-native android plugin in gradle is already providing quite a bit of options to define paths to its options, so my look like this:

react {
    root = file("../reactnativemodule/")
    reactNativeDir = file("../reactnativemodule/node_modules/react-native")
    codegenDir = file("../reactnativemodule/node_modules/@react-native/codegen")
    cliFile = file("../reactnativemodule/node_modules/react-native/cli.js")
    entryFile = file("../reactnativemodule/index.js")
}

The Android project contains those plugin defintions but nothing else except actually consuming the rn-module.

Of course the project based gradle contains the react-native gradle plugin defintion and the root-project plugin usage.

buildscript {
    ext {
        buildToolsVersion = "34.0.0"
        minSdkVersion = 26
        compileSdkVersion = 34
        targetSdkVersion = 34
        ndkVersion = "26.1.10909125"
        kotlinVersion = "1.9.0"
    }
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath("com.android.tools.build:gradle")
        classpath("com.facebook.react:react-native-gradle-plugin")
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.0")
    }
}

apply plugin: "com.facebook.react.rootproject"

The rn-module contains the actual gradle dependencies:

api("com.facebook.react:react-android")
implementation("com.facebook.react:hermes-android")

Additionally the rn-module also contains apply from: file("../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project). This is to ensure that the generated package list file is created, but just for the module, not for the android base project to avoid mixing these projects too much. Because the node_modules and everything around that is not part of the android base project, running native_modules.gradle actually fails with a gradle exception which relates to a missing node module.

Caused by: java.lang.Exception: node:internal/modules/cjs/loader:1137 throw err; ^Error: Cannot find module 'react-native/cli'Require stack:- /Users/alex.eggers/Documents/Projects/RNAndroid/[eval] at Module._resolveFilename (node:internal/modules/cjs/loader:1134:15) at Module._load (node:internal/modules/cjs/loader:975:27) at Module.require (node:internal/modules/cjs/loader:1225:19) at require (node:internal/modules/helpers:177:18) at [eval]:1:87 at Script.runInThisContext (node:vm:122:12) at Object.runInThisContext (node:vm:298:38) at node:internal/process/execution:82:21 at [eval]-wrapper:6:24 at runScript (node:internal/process/execution:81:62) { code: 'MODULE_NOT_FOUND', requireStack: [ '/Users/alex.eggers/Documents/Projects/RNAndroid/[eval]' ]}Node.js v18.19.0

The fix that I've applied is:

diff --git a/node_modules/@react-native-community/cli-platform-android/native_modules.gradle b/node_modules/@react-native-community/cli-platform-android/native_modules.gradle
index bbfa7f7..e7a38f7 100644
--- a/node_modules/@react-native-community/cli-platform-android/native_modules.gradle
+++ b/node_modules/@react-native-community/cli-platform-android/native_modules.gradle
@@ -484,7 +484,7 @@ class ReactNativeModules {
  * Sometimes Gradle can be called outside of JavaScript hierarchy. Detect the directory
  * where build files of an active project are located.
  */
-def projectRoot = rootProject.projectDir
+def projectRoot = new File(rootProject.projectDir.path + "/reactnativemodule")
 
 def autoModules = new ReactNativeModules(logger, projectRoot)

So my question is if there is a solution out there which avoids having that patch in place or is that simply something the current CLI is not supporting in its setup?

@AlexanderEggers AlexanderEggers added the question Further information is requested label Feb 9, 2024
@szymonrybczak
Copy link
Collaborator

hey @AlexanderEggers, as of my knowledge there's no unfortunately. I'll try to find a way to set this path correctly.

@szymonrybczak
Copy link
Collaborator

hey @AlexanderEggers, can you please try setting projectDir through settings.gradle?

diff --git a/my_wraper/android/gradlew b/my_wraper/android/gradlew
old mode 100644
new mode 100755
diff --git a/my_wraper/android/settings.gradle b/my_wraper/android/settings.gradle
index 9694a74..f3259bf 100644
--- a/my_wraper/android/settings.gradle
+++ b/my_wraper/android/settings.gradle
@@ -1,4 +1,5 @@
 rootProject.name = 'AwesomeProject'
+rootProject.projectDir = new File(rootProject.projectDir, "../reactnativemodule")
 apply from: file("../reactnativemodule/node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesSettingsGradle(settings)
 include ':app'
 includeBuild('../reactnativemodule/node_modules/@react-native/gradle-plugin')

@AlexanderEggers
Copy link
Author

AlexanderEggers commented Feb 18, 2024

@szymonrybczak Thanks for your suggestion but I'm not certain if that is what would make sense in this case. The base Android project still needs to be compiled and is indeed the root project. The missing option is to define the RN root project so that the native modules gradle file resolves correctly.

Something like injecting a custom path as part of this line file("../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project).

@AlexanderEggers
Copy link
Author

@szymonrybczak I have open a PR with some suggestions which could fix the mentioned issue (#2341).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants