cli: react-native/cli not found error caused by `use_native_modules!`

Description

iOS

After upgrading our brownfield project from 0.59.9 to 0.62.0 I encounter the following error when trying to run pod install:

> pod install

[!] Invalid `Podfile` file: [!] /Users/dude/.nvm/versions/node/v12.16.1/bin/node -e try {console.log(require('@react-native-community/cli').bin);} catch (e) {consol
e.log(require('react-native/cli').bin);}

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

Error: Cannot find module 'react-native/cli'
Require stack:
- /myapp/ios/[eval]
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:982:15)
    at Function.Module._load (internal/modules/cjs/loader.js:864:27)
    at Module.require (internal/modules/cjs/loader.js:1044:19)
    at require (internal/modules/cjs/helpers.js:77:18)
    at [eval]:1:87
    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:1158:30)
    at evalScript (internal/process/execution.js:94:25) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/myapp/ios/[eval]' ]
}
.

 #  from /myapp/ios/Podfile:112
 #  -------------------------------------------
 #  
 >      use_native_modules!
 #      # Enables Flipper.
 #  -------------------------------------------

This error is caused by the following line in Podfile:

use_native_modules!

Going up the stream, I noticed that the error comes from this line in node_modules/@react-native-community/cli-platform-ios/native-modules.rb:

cli_resolve_script = "try {console.log(require('@react-native-community/cli').bin);} catch (e) {console.log(require('react-native/cli').bin);}"

After commenting out the use_native_modules! line in Podfile, the project builds and runs fine The unwanted side-effect is that autolinking doesn’t seem to work anymore

Android

A similar error happens on Android when doing gradle sync:

Gradle sync failed: Cause: internal/modules/cjs/loader.js:985  throw err;  
^Error: Cannot find module 'react-native/cli'Require stack:- /myapp/android/[eval]    
  at Function.Module._resolveFilename (internal/modules/cjs/loader.js:982:15)    
  at Function.Module._load (internal/modules/cjs/loader.js:864:27)    
  at Module.require (internal/modules/cjs/loader.js:1044: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:1158:30)    
  at evalScript (internal/process/execution.js:94:25) {  code: 'MODULE_NOT_FOUND',  requireStack: [    '/myapp/android/[eval]'  ]}
				Consult IDE log for more details (Help | Show Log) (256 ms)

This error is caused by the following line in settings.gradle:

apply from: file("../src/node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesSettingsGradle(settings)

Again going up the stream, I noticed that the error comes from this line in node_modules/@react-native-community/cli-platform-android/native-modules.gradle:

def cliResolveScript = "console.log(require('react-native/cli').bin);"

And, like iOS, after commenting out the apply from: line in settings.gradle, the project builds and runs fine. The unwanted side-effect here, as well, is that it seems like autolinking doesn’t work anymore

Environment

> yarn exec react-native info
yarn exec v1.22.4
info Fetching system and libraries information...
System:
    OS: macOS 10.15.4
    CPU: (8) x64 Intel(R) Core(TM) i7-8569U CPU @ 2.80GHz
    Memory: 874.98 MB / 16.00 GB
    Shell: 3.2.57 - /bin/bash
  Binaries:
    Node: 12.16.1 - /var/folders/bg/l9cbxs955zsdlj3gl697fsk80000gp/T/yarn--1587152442523-0.833201365558697/node
    Yarn: 1.22.4 - /var/folders/bg/l9cbxs955zsdlj3gl697fsk80000gp/T/yarn--1587152442523-0.833201365558697/yarn
    npm: 6.13.4 - ~/.nvm/versions/node/v12.16.1/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  Managers:
    CocoaPods: 1.9.1 - /usr/local/bin/pod
  SDKs:
    iOS SDK:
      Platforms: iOS 13.4, DriverKit 19.0, macOS 10.15, tvOS 13.4, watchOS 6.2
    Android SDK: Not Found
  IDEs:
    Android Studio: 3.6 AI-192.7142.36.36.6241897
    Xcode: 11.4/11E146 - /usr/bin/xcodebuild
  Languages:
    Java: 1.8.0_102 - /usr/bin/javac
    Python: 2.7.16 - /usr/bin/python
  npmPackages:
    @react-native-community/cli: Not Found
    react: 16.11.0 => 16.11.0 
    react-native: 0.62.0 => 0.62.0 
  npmGlobalPackages:
    *react-native*: Not Found
✨  Done in 2.34s.
  • The structure of the project is:
myapp  __ ios/
      |__ android/
      |__ src/ __ index.js
              |__ package.json
              |__ node_modules/
  • I uninstalled the global react-native-cli as per recommendations in the README file.

react-native-cli – an optional global convenience package, which is a proxy to @react-native-community/cli and global installation helper. Please consider it legacy, because it’s not necessary anymore.

I use npx or yarn exec for all react-native commands, to use locally installed binaries.

BTW, the error stays the same with of without the global react-native-cli installed

  • I followed the instructions in the README Updating the CLI section to bring all the cli packages up-to-date:
> yarn list --pattern @react-native-community/cli
yarn list v1.22.4
├─ @react-native-community/cli-debugger-ui@4.7.0
├─ @react-native-community/cli-platform-android@4.7.0
├─ @react-native-community/cli-platform-ios@4.7.0
├─ @react-native-community/cli-tools@4.7.0
├─ @react-native-community/cli-types@4.7.0
└─ @react-native-community/cli@4.7.0
✨  Done in 0.73s.

Steps To Reproduce

I tried to reproduce with a brand new test project created via, using the exact same environment

yarn exec react-native init New0620 --version 0.620

but, I can’t reproduce neither the iOS nor the Android issue.

Expected Results

I should be able to uncomment use_native_modules! in Podfile and run pod install without this error I should be able to uncomment apply from: file("../src/node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesSettingsGradle(settings) in settings.gradle and be able to run the gradle sync without this error

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 4
  • Comments: 27 (7 by maintainers)

Most upvoted comments

Doing the exact opposite of the recommendations fixed this for me. I installed @react-native-community/cli. For some reason for upgraded projects @react-native-community/cli isn’t imported correctly.

npm i --save @react-native-community/cli

You know what they say when you are desperate to hit a deadline yolo this shit.

In my case, it was failing on node 14 but working on node 10. It is just a temporary solution, I’m trying to find a fixed solution.

Nope 😞 No idea even where to begin to fix it 😕

None of the solutions suggested here fixed my issue 😢 which is not exactly what’s described here but seems closely related. For me when I run npx pod-install ios it gets stuck forever. I went to the ios folder, ran pod install --verbose, and it’s in some kind of loop:

  $ /usr/local/bin/node -e try {console.log(require('@react-native-community/cli').bin);} catch (e)
  {console.log(require('react-native/cli').bin);}
  /Users/sergiou87/Development/ExampleRN/node_modules/react-native/node_modules/@react-native-community/cli/build/bin.js
  $ /usr/local/bin/node -e try {console.log(require('@react-native-community/cli').bin);} catch (e)
  {console.log(require('react-native/cli').bin);}
  /Users/sergiou87/Development/ExampleRN/node_modules/react-native/node_modules/@react-native-community/cli/build/bin.js
  $ /usr/local/bin/node -e try {console.log(require('@react-native-community/cli').bin);} catch (e)
  {console.log(require('react-native/cli').bin);}
  /Users/sergiou87/Development/ExampleRN/node_modules/react-native/node_modules/@react-native-community/cli/build/bin.js
  $ /usr/local/bin/node -e try {console.log(require('@react-native-community/cli').bin);} catch (e)
  {console.log(require('react-native/cli').bin);}
  /Users/sergiou87/Development/ExampleRN/node_modules/react-native/node_modules/@react-native-community/cli/build/bin.js
  $ /usr/local/bin/node -e try {console.log(require('@react-native-community/cli').bin);} catch (e)
  {console.log(require('react-native/cli').bin);}
  ...

Easy way to repro this is trying to install React Navigation in a clean React Native 0.62 project:

$ npx react-native init ExampleRN --version=0.62
$ cd ExampleRN
$ npm install @react-navigation/native
$ npm install react-native-reanimated react-native-gesture-handler react-native-screens react-native-safe-area-context @react-native-community/masked-view
$ npx pod-install ios

If I run npx pod-install ios right after installing @react-navigation/native, that one works fine, but after the last npm install command it doesn’t work.

This used to work fine for me 2-3 weeks ago.

Please make sure you have all JS deps installed (run yarn --force or remove node_modules and try again). Also, what happens when you run node -e "console.log(require('react-native/cli').bin);"? Maybe there’s some extra output which causes JSON parsing to fail