Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot run program "npx": error=2, No such file or directory when running React Native app

I have been working with react-native project in another pc and it has been working correctly. Now I cloned project to another device where I have react-native installed since I work with other project and I cannot run it.

When I run react-native run-android I get this error:

Cannot run program "npx": error=2, No such file or directory

like image 777
Edison Biba Avatar asked Dec 28 '19 13:12

Edison Biba


3 Answers

I'm using Android Studio 4 on Mac OS Catalina 10.15.6. I solved the problem by running Android Studio with bellow command on console.

$ open -a "Android Studio.app"
like image 60
NinjaDev Avatar answered Oct 22 '22 19:10

NinjaDev


Simple steps you need to go through to make it work with npx

  • sudo npm uninstall -g react-native-cli
  • sudo npm i -g npx
  • npx react-native run-android

More detailed explanation why this is happenig

Issue was that Facebook is not using anymore react-native-cli as they are using npx.

As stated in Facebook page:

If you previously installed a global react-native-cli package, please remove it as it may cause unexpected issues.

So i had to run sudo npm uninstall -g react-native-cli to remove react-native-cli

They also say that npx is shipped with nodejs but that wasn't my case.

React Native has a built-in command line interface, which you can use to generate a new project. You can access it without installing anything globally using npx, which ships with Node.js.

So I had to install it using sudo npm i -g npx

After installing npx then just need to run npx react-native run-android.

Be aware of npx before react-native run-android

Now you don't have any command react-native now we only have 'npx' and react-native run-android is just a parameter for npx. For ex. to start metro we should run npx react-native start

like image 21
Edison Biba Avatar answered Oct 22 '22 19:10

Edison Biba


I had the same issues, so I followed these steps:

  1. Start by installing the latest version of Android Studio (Don't delete the old version, just copy paste that to another directory or in my case I just added the Android Studio 3.2.1 that I had to a folder called olderAndroidStudio, on mac).
  2. Make sure you don't click on import settings from Existing.
  3. Follow along the installation, and complete it.
  4. Build and Run, app runs without any problems.

I additionally uninstalled react-native-cli using the method given in the accepted answer.

like image 1
Kiran Avatar answered Oct 22 '22 19:10

Kiran