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

5

7

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

Noon answered 28/12, 2019 at 13:29 Comment(2)
What npm version are you using? npm -vMalayalam
If you use Ubuntu or Mac OS, please run Android Studio with below command on Terminal. ` $ open -a "Android Studio.app" ` After that, build your react-native android app on the Android Studio.Undersheriff
N
7

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

Noon answered 28/12, 2019 at 13:41 Comment(0)
U
9

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"
Undersheriff answered 6/8, 2020 at 1:38 Comment(3)
It would be great to know why this helps, but it helps all the same. Thank youZubkoff
since I am pretty sure you have your .env variables in your terminal profile that is pointing to androidSunday
open -a /Applications/Android\ Studio.appHollenbeck
N
7

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

Noon answered 28/12, 2019 at 13:41 Comment(0)
D
1

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.

Domingodominguez answered 26/5, 2020 at 10:15 Comment(1)
Actually, this worked for me, I just update my android studio and then restart android studio!!!Tacitus
S
0

In my case, I just quit Android Studio and reopened, and the error was gone.

Maybe throw in a ./gradlew clean for goo measure.

Showdown answered 26/5, 2021 at 1:5 Comment(0)
P
0

I solved it by finding the command that was throwing error and ran it directly

node -e "console.log(require('react-native/cli').bin)"

which outputs a path, something like

$HOME/my-app/node_modules/@react-native-community/cli/build/bin.js

then I pasted that path in android/app/build.gradle

project.ext.react = [
    entryFile: "index.js",
    enableHermes: true,  // clean and rebuild if changing
    cliPath: "$HOME/my-app/node_modules/@react-native-community/cli/build/bin.js" // <--- add this
]
Potvaliant answered 10/1, 2022 at 8:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.