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

Environment

System: OS: macOS 12.3.1 CPU: (12) x64 Intel® Core™ i7-9750H CPU @ 2.60GHz Memory: 809.65 MB / 16.00 GB Shell: 3.2.57 - /bin/bash Binaries: Node: 12.15.0 - /usr/local/bin/node Yarn: 1.22.18 - /usr/local/bin/yarn npm: 6.13.4 - /usr/local/bin/npm Watchman: 2022.03.21.00 - /usr/local/bin/watchman Managers: CocoaPods: 1.11.3 - /usr/local/bin/pod SDKs: iOS SDK: Platforms: DriverKit 21.2, iOS 15.2, macOS 12.1, tvOS 15.2, watchOS 8.3 Android SDK: Not Found IDEs: Android Studio: 2021.1 AI-211.7628.21.2111.8309675 Xcode: 13.2/13C90 - /usr/bin/xcodebuild Languages: Java: 12.0.2 - /usr/bin/javac npmPackages: @react-native-community/cli: Not Found react: Not Found react-native: 0.66.4 react-native-macos: Not Found npmGlobalPackages: react-native: Not Found

Description

I am trying to upgrade the react-native version from 0.63 to 0.66 but I am getting this below error, already tried deleting node_modules, adding cli as dependency in package.json, tried with different gradle version, different npm and node versions.

` Starting a Gradle Daemon (subsequent builds will be faster) :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 ‘node_modules/@react-native-community/cli-platform-android/native_modules.gradle’ line: 213

  • What went wrong: A problem occurred evaluating script.

internal/modules/cjs/loader.js:796 throw err; ^Error: Cannot find module 'react-native/cli’Require stack:- app/android-app/[eval] at Function.Module._resolveFilename (internal/modules/cjs/loader.js:793:17) at Function.Module._load (internal/modules/cjs/loader.js:686:27) at Module.require (internal/modules/cjs/loader.js:848:19) at require (internal/modules/cjs/helpers.js:74:18) at [eval]:1:13 at Script.runInThisContext (vm.js:116:20) at Object.runInThisContext (vm.js:306:38) at Object.<anonymous> ([eval]-wrapper:9:26) at Module._compile (internal/modules/cjs/loader.js:955:30) at evalScript (internal/process/execution.js:80:25) { code: ‘MODULE_NOT_FOUND’, requireStack: [ ‘app/android-app/[eval]’ ]} `

but when running the command im my terminal, getting this file `

node -e “console.log(require(‘react-native/cli’).bin);” /Users/Desktop/app/android-app/node_modules/react-native/node_modules/@react-native-community/cli/build/bin.js `

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 16
  • Comments: 19

Most upvoted comments

Did you resolve this problem at last? I caught the same.

Hello Could you please check which version of node you’re using? Also, maybe node_modules are corrupted? Could you please try to remove them (and also yarn.lock / package-lock.json) and reinstall dependencies?

same problem as 0.68.2, when move android dir outer react-native js project use relative path

react-native@0.68.2:
  version "0.68.2"
  resolved "https://registry.yarnpkg.com/react-native/-/react-native-0.68.2.tgz#07547cd31bb9335a7fa4135cfbdc24e067142585"
  integrity sha512-qNMz+mdIirCEmlrhapAtAG+SWVx6MAiSfCbFNhfHqiqu1xw1OKXdzIrjaBEPihRC2pcORCoCHduHGQe/Pz9Yuw==
  dependencies:
    "@jest/create-cache-key-function" "^27.0.1"
    "@react-native-community/cli" "^7.0.3"
    "@react-native-community/cli-platform-android" "^7.0.1"
    "@react-native-community/cli-platform-ios" "^7.0.1"
~/Desktop/android ❯ ./gradlew clean --info                                                                                                                                 2.7.4 00:49:44
Initialized native services in: /Users/kr.mao/.gradle/native
Initialized jansi services in: /Users/kr.mao/.gradle/native
The client will now receive all logging from the daemon (pid: 10648). The daemon log file: /Users/kr.mao/.gradle/daemon/7.3.3/daemon-10648.out.log
Starting 28th build in daemon [uptime: 1 hrs 38 mins 9.32 secs, performance: 100%, non-heap usage: 39% of 512 MiB]
Using 8 worker leases.
Now considering [/Users/kr.mao/Desktop/android, /Users/kr.mao/Workspace/rn_addtoapp_bridge/example/node_modules/react-native-gradle-plugin, /Users/kr.mao/Workspace/rn_addtoapp_bridge/example/android] as hierarchies to watch
Watching the file system is configured to be enabled if available
File system watching is active
Starting Build
:settings:reactNativeBusinessRootRelativePath=../../Workspace/rn_addtoapp_bridge/example
:ReactNative:Unexpected empty result of running '[node, -e, try {console.log(require('@react-native-community/cli').bin);} catch (e) {console.log(require('react-native/cli').bin);}]' command.
:ReactNative:Running '[node, -e, try {console.log(require('@react-native-community/cli').bin);} catch (e) {console.log(require('react-native/cli').bin);}]' command failed.

FAILURE: Build failed with an exception.

* Where:
Script '/Users/kr.mao/Workspace/rn_addtoapp_bridge/example/node_modules/@react-native-community/cli-platform-android/native_modules.gradle' line: 213

* What went wrong:
A problem occurred evaluating script.
> node:internal/modules/cjs/loader:936  throw err;  ^Error: Cannot find module 'react-native/cli'Require stack:- /Users/kr.mao/Desktop/android/[eval]    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)    at Function.Module._load (node:internal/modules/cjs/loader:778:27)    at Module.require (node:internal/modules/cjs/loader:1005:19)    at require (node:internal/modules/cjs/helpers:102:18)    at [eval]:1:87    at Script.runInThisContext (node:vm:129:12)    at Object.runInThisContext (node:vm:305:38)    at node:internal/process/execution:75:19    at [eval]-wrapper:6:22    at evalScript (node:internal/process/execution:74:60) {  code: 'MODULE_NOT_FOUND',  requireStack: [ '/Users/kr.mao/Desktop/android/[eval]' ]}

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

I found the key in native_modules.gradle

ext.applyNativeModulesSettingsGradle = { DefaultSettings defaultSettings, String root = null ->
  if (root != null) {
    logger.warn("${ReactNativeModules.LOG_PREFIX}Passing custom root is deprecated. CLI detects root automatically now.");
    logger.warn("${ReactNativeModules.LOG_PREFIX}Please remove second argument to `applyNativeModulesSettingsGradle`.");
  }
  autoModules.addReactNativeModuleProjects(defaultSettings)
}

ext.applyNativeModulesAppBuildGradle = { Project project, String root = null ->
  if (root != null) {
    logger.warn("${ReactNativeModules.LOG_PREFIX}Passing custom root is deprecated. CLI detects root automatically now");
    logger.warn("${ReactNativeModules.LOG_PREFIX}Please remove second argument to `applyNativeModulesAppBuildGradle`.");
  }

same project with different dir got different output

~/Workspace/rn_addtoapp_bridge/example/android master !1 ❯ pwd                                                                                                         7s  2.7.4 02:24:05
/Users/kr.mao/Workspace/rn_addtoapp_bridge/example/android
~/Workspace/rn_addtoapp_bridge/example/android master !1 ❯ node /Users/kr.mao/Desktop/android/../../Workspace/rn_addtoapp_bridge/example/node_modules/@react-native-community/cli/build/bin.js config
{
  "root": "/Users/kr.mao/Workspace/rn_addtoapp_bridge/example",
  "reactNativePath": "/Users/kr.mao/Workspace/rn_addtoapp_bridge/example/node_modules/react-native",
  "dependencies": {
    "rn_addtoapp_bridge": {
      "root": "/Users/kr.mao/Workspace/rn_addtoapp_bridge",
      "name": "rn_addtoapp_bridge",
      "platforms": {
        "ios": {
~/Desktop/android ❯ pwd                                                                                                                                             ✘ INT  2.7.4 02:37:10
/Users/kr.mao/Desktop/android
~/Desktop/android ❯ node /Users/kr.mao/Desktop/android/../../Workspace/rn_addtoapp_bridge/example/node_modules/@react-native-community/cli/build/bin.js config             2.7.4 02:37:12
~/Desktop/android ❯                                                                                                                                                        2.7.4 02:37:13

this will force people use parent dir node_modules, can’t custom clipath now. integrate rn to exsting app, we really need a different js dir, not must be a parent dir. thanks. can some way resolve this? now I can only with patch-package

图像 projectRoot 是固定的,需要修改为如下: 图像

After I have reformed this way, I have encountered such mistakes: WechatIMG15344

Is this problem solved, brother? I also had this problem integrating 0.72.3

Hello,

Any fix about this issue. I can reproduce with react-native": “0.68.2” 😢