cli: Cannot find module 'react-native/cli'

Environment

react-native info output
λ yarn exec react-native info
yarn exec v1.22.0
info Fetching system and libraries information...
System:
    OS: macOS Mojave 10.14.6
    CPU: (4) x64 Intel(R) Core(TM) i5-4308U CPU @ 2.80GHz
    Memory: 52.27 MB / 16.00 GB
    Shell: 3.0.2 - /usr/local/bin/fish
  Binaries:
    Node: 12.16.0 - /var/folders/z8/xq1r3qcd7szbpx1_77xsgpqc0000gn/T/yarn--1582222260147-0.32330923283802093/node
    Yarn: 1.22.0 - /var/folders/z8/xq1r3qcd7szbpx1_77xsgpqc0000gn/T/yarn--1582222260147-0.32330923283802093/yarn
    npm: 6.13.4 - /usr/local/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  SDKs:
    iOS SDK:
      Platforms: iOS 12.4, macOS 10.14, tvOS 12.4, watchOS 5.3
    Android SDK:
      API Levels: 23, 25, 26, 27, 28
      Build Tools: 23.0.1, 25.0.2, 25.0.3, 26.0.1, 26.0.2, 27.0.2, 27.0.3, 28.0.3
      System Images: android-27 | Google APIs Intel x86 Atom
  IDEs:
    Android Studio: 3.1 AI-173.4720617
    Xcode: 10.3/10G8 - /usr/bin/xcodebuild
  npmPackages:
    react: ~16.9.0 => 16.9.0 
    react-native: ~0.61.5 => 0.61.5
✨  Done in 4.79s.

Description

Hi folks, this issue is very closely related to #406, but I decided to open a new issue for it since it may require a different solution altogether, and that issue is closed.

In short, @react-native-community/cli-platform-android is breaking our builds on Appcenter (cc @kinigitbyday https://github.com/react-native-community/cli/issues/406#issuecomment-588406396 ), using the following versions:

λ yarn list --pattern @react-native-community/cli
yarn list v1.22.0
├─ @react-native-community/cli-debugger-ui@3.0.0
├─ @react-native-community/cli-platform-android@3.1.4
├─ @react-native-community/cli-platform-ios@3.2.0
├─ @react-native-community/cli-tools@3.0.0
├─ @react-native-community/cli-types@3.0.0
└─ @react-native-community/cli@3.2.0

The exact failure logged by Appcenter (aka Azure DevOps) is the following:

[command]/Users/runner/runners/2.165.0/work/1/s/app/android/gradlew -DAPPCENTER_KEYSTORE_PASSWORD=*** -DMOBILECENTER_KEYSTORE_PASSWORD=*** -DAPPCENTER_KEY_ALIAS=*** -DMOBILECENTER_KEY_ALIAS=*** -DAPPCENTER_KEY_PASSWORD=*** -DMOBILECENTER_KEY_PASSWORD=*** -DAPPCENTER_BUILD_VERSION=14 -DMOBILECENTER_BUILD_VERSION=14 -p app/android clean :app:assembleRelease :app:bundleRelease
Starting a Gradle Daemon, 1 incompatible Daemon could not be reused, use --status for details
Gradle 5.5
:ReactNative:Unexpected empty result of running '[node, -e, console.log(require('react-native/cli').bin);]' command.
:ReactNative:Running '[node, -e, console.log(require('react-native/cli').bin);]' command failed.

FAILURE: Build failed with an exception.

* Where:
Script '/Users/runner/runners/2.165.0/work/1/s/app/node_modules/@react-native-community/cli-platform-android/native_modules.gradle' line: 170

* What went wrong:
A problem occurred evaluating script.
> internal/modules/cjs/loader.js:984  throw err;  ^Error: Cannot find module 'react-native/cli'Require stack:- /Users/runner/runners/2.165.0/work/1/s/[eval]    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:981:15)    at Function.Module._load (internal/modules/cjs/loader.js:863:27)    at Module.require (internal/modules/cjs/loader.js:1043:19)    at require (internal/modules/cjs/helpers.js:77:18)    at [eval]:1:13    at Script.runInThisContext (vm.js:120:20)    at Object.runInThisContext (vm.js:311:38)    at Object.<anonymous> ([eval]-wrapper:10:26)    at Module._compile (internal/modules/cjs/loader.js:1157:30)    at evalScript (internal/process/execution.js:94:25) {  code: 'MODULE_NOT_FOUND',  requireStack: [ '/Users/runner/runners/2.165.0/work/1/s/[eval]' ]}

There is a comment in the affected Gradle file Sometimes Gradle can be called outside of the JavaScript hierarchy (-p flag) which seems related. Appcenter calls this with -p app/android.

We have our RN app in a directory called app/ in our repo, and you can see from the stacktrace that the eval is executed from the root directory of the repo instead of within app/ or app/android as I would expect.

I have tried various versions of @react-native-community/cli-platform-android, and it seems that the issue was introduced by this commit which removes npx, which was released in 3.1.0: https://github.com/react-native-community/cli/commit/b4b08545d3ccec3d2d4a545f029496e0358de49a

We hotfixed this for now by pinning @react-native-community/cli-platform-android to the latest version before 3.1.0 by adding the following to our package.json:

  "resolutions": {
    "@react-native-community/cli-platform-android": "~3.0.0"
  }

Hope this helps, let me know if I can help test any solution to this 😃 Thank you!

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 30 (9 by maintainers)

Commits related to this issue

Most upvoted comments

Hi @thymikee, thanks for the quick response. Our React Native package.json file is in a subdirectory called app within the main repo which also has a package.json for server / web stuff. Gradle is called from app/android/gradlew.

Unfortunately I spoke to soon regarding downgrading the version, that ultimately failed too.

I saw that the getCommandOutput method no longer sets a current working directory, which was done in #852. I’m not entirely sure I understand the reasoning for this, but just as a quick hack to get our build working again, I tried to set the working directory to jsAppDir (I just applied this using patch-package):

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 8f5dc40..06f6e6c 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
@@ -148,10 +148,10 @@ class ReactNativeModules {
    * Runs a specified command using Runtime exec() in a specified directory.
    * Throws when the command result is empty.
    */
-  String getCommandOutput(String[] command) {
+  String getCommandOutput(String[] command, File directory) {
     try {
       def output = ""
-      def cmdProcess = Runtime.getRuntime().exec(command)
+      def cmdProcess = Runtime.getRuntime().exec(command, null, directory)
       def bufferedReader = new BufferedReader(new InputStreamReader(cmdProcess.getInputStream()))
       def buff = ""
       def readBuffer = new StringBuffer()
@@ -194,10 +194,10 @@ class ReactNativeModules {
      */
     def cliResolveScript = "console.log(require('react-native/cli').bin);"
     String[] nodeCommand = ["node", "-e", cliResolveScript]
-    def cliPath = this.getCommandOutput(nodeCommand)
+    def cliPath = this.getCommandOutput(nodeCommand, new File(this.jsAppDir))
 
     String[] reactNativeConfigCommand = ["node", cliPath, "config"]
-    def reactNativeConfigOutput = this.getCommandOutput(reactNativeConfigCommand)
+    def reactNativeConfigOutput = this.getCommandOutput(reactNativeConfigCommand, new File(this.jsAppDir))
 
     def json
     try {

This seems to be working for us so far, though I’m not sure what other side effects this could have. I believe it could potentially solve #804 too, though I haven’t looked into that too deeply.

Thank you!

I solved it by upgrading my project from React Native 0.61 -> 0.62.2, because 0.62.2 uses up-to-date-enough version of the cli:

    "@react-native-community/cli": "^4.5.1",
    "@react-native-community/cli-platform-android": "^4.5.1",
    "@react-native-community/cli-platform-ios": "^4.5.0",

https://github.com/facebook/react-native/blob/v0.62.2/package.json#L89-L91

Thanks a lot for fixing the bug initially 👍

I am also facing this issue after I upgraded my react-native version from 0.60.4 to 0.61.5 and this error I am seeing on app center only.

I don’t have any config, it’s done totally their ci. i.e. image

Another workaround for this issue is to put something like this in the pre-build script.

cp -a "$APPCENTER_SOURCE_DIRECTORY/mobile/" $APPCENTER_SOURCE_DIRECTORY

Of course, instead of /mobile put your RN directory path inside the repo.

So after fix #1057, who should upgrade what to which version, to make this work in appcenter.ms? Is it react-native that should be upgraded?

@fabienso Android builds have been failing in App Center since 09:30 UTC and the status page still reports Experiencing Issues. Maybe try again once this has been resolved?

Is it fixed? Or any real fix here? I am not able to build it in AppCenter. I am using yarn workspaces