cli icon indicating copy to clipboard operation
cli copied to clipboard

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

Open Darsbir opened this issue 2 years ago • 3 comments

Environment

System: OS: macOS 12.3.1 CPU: (12) x64 Intel(R) Core(TM) 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. ([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 `

Darsbir avatar Apr 29 '22 09:04 Darsbir

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

CPPAlien avatar Aug 08 '22 08:08 CPPAlien

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

[email protected]:
  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

krmao avatar Aug 27 '22 17:08 krmao

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

[email protected]:
  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

krmao avatar Aug 27 '22 18:08 krmao

Hello,

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

sanders9 avatar Oct 31 '22 17:10 sanders9

Same situation with "react-native": "0.67.5"

javierconstanzo-san avatar Dec 08 '22 15:12 javierconstanzo-san

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?

adamTrz avatar Dec 08 '22 19:12 adamTrz

Hey this is node v16.16.0 and this is a clean project started from scratch. This is not related to node_modules or node, it's use_native_modules not working properly. See this and this for details.

javierconstanzo-san avatar Dec 09 '22 14:12 javierconstanzo-san

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

[email protected]:
  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

I am right, for 0.71.0 node_modules/react-native-gradle-plugin/src/main/kotlin/com/facebook/react/ReactExtension.kt

val root: DirectoryProperty =
      objects.directoryProperty().convention(project.rootProject.layout.projectDirectory.dir("../"))

code force the location, then native code can not build on other path outer react-native-business project

krmao avatar Jan 16 '23 06:01 krmao

+1

whuthj avatar Feb 25 '23 04:02 whuthj

+1

Malin88 avatar Apr 06 '23 09:04 Malin88

+1

TheHunterDog avatar Apr 06 '23 14:04 TheHunterDog

Use the built-in macOS Terminal.

TheHunterDog avatar Apr 06 '23 19:04 TheHunterDog

even when i am inside react native project, and run react-native run-android or ./gradlew clean i am getting error:

  • Where: Script ‘/Users/sudeep/Downloads/vuc-dev/vuc-mobile/node_modules/@react-native-community/cli-platform-android/native_modules.gradle’ line: 170
> node:internal/modules/cjs/loader:936  throw err;  ^Error: Cannot find module ’react-native/cli’Require stack:- /Users/sudeep/.gradle/daemon/5.5/[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:13    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/sudeep/.gradle/daemon/5.5/[eval]’ ]}

rohitsingh2410 avatar Apr 18 '23 15:04 rohitsingh2410

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

helloMrTom avatar Aug 17 '23 06:08 helloMrTom

image projectRoot is fixed and needs to be modified to like this: image

jiantao88 avatar Aug 17 '23 07:08 jiantao88

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

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

helloMrTom avatar Aug 17 '23 08:08 helloMrTom

I have this problem too, https://github.com/facebook/react-native/issues/41244#issuecomment-1790981785 Here is the simplest demo . https://github.com/woshi82/rn-locate-symbol-error.git

After reading this problem, I try to modified the code in E:\AwesomeProject\rn\node_modules\@react-native-community\cli-platform-android\native_modules.gradle from

// number: 489 line
/*
 * 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

to

def projectRoot = new File(rootProject.projectDir, "../../rn/node_modules")

It's seems worked. But there is a chain reaction occurring in a third-party library. (The error shows bellow) That's not a comprehensive solution。

Build file 'E:\AwesomeProject\rn\node_modules\@react-native-camera-roll\camera-roll\android\build.gradle' line: 90

A problem occurred evaluating project ':react-native-camera-roll_camera-roll'.
> [react-native-cameraroll] Unable to resolve react-native location in node_modules. You should add project extension property (in app/build.gradle) `REACT_NATIVE_NODE_MODULES_DIR` with path to react-native.

* Try:
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

Anyone has good idea?

woshi82 avatar Nov 03 '23 03:11 woshi82

I have this problem too, facebook/react-native#41244 (comment) Here is the simplest demo . https://github.com/woshi82/rn-locate-symbol-error.git

After reading this problem, I try to modified the code in E:\AwesomeProject\rn\node_modules\@react-native-community\cli-platform-android\native_modules.gradle from

// number: 489 line
/*
 * 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

to

def projectRoot = new File(rootProject.projectDir, "../../rn/node_modules")

It's seems worked. But there is a chain reaction occurring in a third-party library. (The error shows bellow) That's not a comprehensive solution。

Build file 'E:\AwesomeProject\rn\node_modules\@react-native-camera-roll\camera-roll\android\build.gradle' line: 90

A problem occurred evaluating project ':react-native-camera-roll_camera-roll'.
> [react-native-cameraroll] Unable to resolve react-native location in node_modules. You should add project extension property (in app/build.gradle) `REACT_NATIVE_NODE_MODULES_DIR` with path to react-native.

* Try:
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

Anyone has good idea?

+1

YASH6004 avatar Dec 12 '23 12:12 YASH6004

There hasn't been any activity on this issue in the past 3 months, so it has been marked as stale and it will be closed automatically if no further activity occurs in the next 7 days.

github-actions[bot] avatar Mar 12 '24 03:03 github-actions[bot]