React Native on Android: Cannot run program "node": error=2, No such file or directory

Androidnode.jsReact Native

Android Problem Overview


EDIT: I am uncertain which steps I took to resolve this problem, and am no longer seeing this error message so I cannot test the solutions below. Please try them individually as different answers seem to be working for different people.

The iOS version works fine, but running Android Studio on my Mac I get the following Gradle Build error (paraphrased):

:ReactNative:Running ‘[node, -e, console.log(require(‘react-native/cli’).bin);]’ command failed.

- Where:
Script ‘/Users/…/Desktop/…/node_modules/@react-native-community/cli-platform-android/native_modules.gradle’ line: 154

- What went wrong:
A problem occurred evaluating script.
> Cannot run program “node”: error=2, No such file or directory

Caused by: java.io.IOException: Cannot run program “node”: error=2, No such file or directory

I've tried so many things:

I used to be able to still run yarn android in Terminal, but even that has stopped working.

Android Solutions


Solution 1 - Android

It's because android studio couldn't find node and should run through terminal.

If you are using Mac you can run Android studio using This command in terminal

1. Run this open -a /Applications/Android\ Studio.app

2. Then sync Gradle

And this will solve the problem.

Solution 2 - Android

In my case with this setup:

  • Apple chip M1
  • Android Studio arctic fox 2020.3.1 Patch 2

I previously ran the command mentioned above, but I kept getting the same error:

sudo ln -s "$(which node)" /usr/local/bin/node

I had to update the gradle version in the project (android/gradle/wrapper/gradle-wrapper.properties)

from:

distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip

to:

distributionUrl=https\://services.gradle.org/distributions/gradle-6.9-all.zip

and execute this command in the project:

cd android && ./gradlew clean

or just delete this folder instead of the executing the previous command: android/.gradle

And after doing these steps I no longer have any problem.

Solution 3 - Android

Simply linking node (installed previously by nvm) to /usr/local/bin/node.

sudo ln -s "$(which node)" /usr/local/bin/node

Ubuntu 20.4, Node 14.15.0

Solution 4 - Android

Solution

Running chmod +x /Applications/Android\ Studio.app/Contents/bin/printenv in the terminal and restarting Android Studio will make the gradle sync work again.

Detail

I am using the latest Android Studio and I did a patch update of the application. My system is running macOS 12.1 on a M1 Pro Apple CPU.

After the restart I could not run my react native application and the gradle sync failed. Any fixes above did not help for me. So I read the gradle log file idea.log in detail. In the very beginning in the log I see the application printenv has some issues.

loading shell env: /bin/zsh -l -i -c '/Applications/Android Studio.app/Contents/bin/printenv' '/var/folders/7y/gz2kx4917yl_804mbfrnkqxc0000gn/T/intellij-shell-env.9046002349811239017.tmp' 
...
.intellij.util.EnvironmentUtil - can't get shell environment 
java.io.IOException: command [/bin/zsh, -l, -i, -c, '/Applications/Android Studio.app/Contents/bin/printenv' '/var/folders/7y/gz2kx4917yl_804mbfrnkqxc0000gn/T/intellij-shell-env.9046002349811239017.tmp']
exit code:126 text:0 out:zsh:1: permission denied: /Applications/Android Studio.app/Contents/bin/printenv

After that I go to folder /Applications/Android Studio.app/Contents/bin/ and list printenv in my terminal and saw that printenv did not have the execute rights:

-rw-r--r--@ 1 user admin 152080 Jan 28 09:12 printenv

Android Studio

Android Studio Bumblebee | 2021.1.1 Build #AI-211.7628.21.2111.8092744, built on January 19, 2022 Runtime version: 11.0.11+0-b60-7772763 aarch64 VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o. macOS 12.1 GC: G1 Young Generation, G1 Old Generation Memory: 2048M Cores: 10 Registry: external.system.auto.import.disabled=true

Solution 5 - Android

For M1 Mac, I downloaded Android Studio (Mac with Apple Chip) version and faced this issue.

It got fixed for me by changing the gradle version to 6.9-rc-1.

This can be done from File -> Project Structure -> Project -> Gradle Version -> Type or select "6.9-rc-1" and click OK button.

You can find similar and more solutions here as well.

https://stackoverflow.com/questions/47018879/gradle-sync-failed-cause-error-2-no-such-file-or-directory#

Solution 6 - Android

Android Studio uses PATH environment variable that is available when the Android Studio process is started up, and your Node binary directory should be in that PATH.

I got this same error in Linux when I had Node installed via n (Node version management tool) which sets the Node path at Bash startup.

The problem in my case was that I started the Android Studio from the desktop and because the Bash startup file was not executed, also the Node path was not set.

The solution was to run Android Studio from the command line.

You could also try to pass the PATH environment variable directly to Android Studio while starting it from the command line:

PATH=$PATH:/path/to/node/bin/ /path/to/android/studio/bin/studio.sh

Solution 7 - Android

I'm on my Mac, and i solve this by close the android studio completely(close the process), and restart it.

Solution 8 - Android

How I solved the same issue.

  1. Open Android studio via terminal
  2. Build Project Like before

Note: I installed node via nvm (I believe that's why I had the issue)

If you installed android studio via snap then its pretty simple to launch it via command line.

Open project folder in terminal and run:

android-studio .

Solution 9 - Android

In android Studio, I solved this problem when it happened to me as follows:

1 - Go to File - Invalidate Caches / Restart; 2 - Choose the option "Invalidate and Restart".

Solution 10 - Android

On a Mac with M1, upgrading Android Studio fixed the issue for me. I upgraded from Arctic Fox to Bumblebee.

Also used this version of Gradle (android/gradle/wrapper/gradle-wrapper.properties):

distributionUrl=https\://services.gradle.org/distributions/gradle-6.9-bin.zip

Solution 11 - Android

I struggled with this a long time on my M1 mac mini. I was getting this error after opening RN android project in Intellij IDEA during gradle sync and also while trying to run ./gradlew commands from command line.

What seems to be a solution for me:

  • rm -rf .gradle (in the RN_PROJECT/android folder)
  • killall java (to stop running gradle daemon)
  • updated node to v16.8.0

I think the real solution is the first step. Now the project opens in IDEA without errors and I can run ./gradlew commands.

Solution 12 - Android

You can kill gradle daemon, or just restart your machine if you made some changes, and it will work.

Solution 13 - Android

I had this issue on M1 mac, found a solution after 3 days, it was because of nvm and .zsh having issues. If you used brew to install it like i did, it's going to cause a lot of issues.

Solution:

I had to uninstall nvm

rm -rf ~/.nvm
rm -rf ~/.npm
rm -rf ~/.bower

Then install nvm via instructions on readme on its github page

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash

Other than that, there is a section about troubleshooting M1 macs, that might help futher if needed. But other than that, Android Studio works fine now without having to open it via terminal or any other work-arounds. (You might have to agree to the sdk license agreement if your build fails, view the build tab for the issue, to agree to the license, just go to Tools > Sdk Manager and uninstall the sdk, click apply and retick it and Apply to reinstall it and it will ask you about the license agreement also.

Solution 14 - Android

Many of these answers are half-right but you must do both of these things:

  1. Make sure that the $PATH variable includes wherever your node is installed. You will will probably set this in .bashrc.

  2. Start Android Studio from the command-line.

Solution 15 - Android

you can use: open your project -> run:

> npx react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle

Solution 16 - Android

I was able to make it work in android studio bumblebee by running this command in terminal on mac,

sudo chmod +x /Applications/Android\ Studio.app/Contents/bin/printenv

and then restart the android studio.

Solution 17 - Android

I have tried most of the answers posted here but the only solution worked was to upgrading the gradle to 6.9 or later.

File -> Project Structure -> Project -> Gradle Version = 6.9(Select version 6.9 or later from dropdown).

Wait for it to download all modules and it should load your project.

Solution 18 - Android

This is not a specific answer to the question, but as some of the comments here helped me resolve my issue, maybe this answer will help someone else.

I was struggling to run a Gradle project using Intellij IDEA on Ubuntu 22.04. It was giving me the same error: Cannot run program "X": error=2, No such file or directory.

Here is what I did to overcome that:

echo 'export PATH=$PATH:/path/to/X' >> ~/.bashrc

echo 'export PATH=$PATH:/path/to/X' >> ~/.profile

sudo echo 'export PATH=$PATH:/path/to/X' >> /etc/environment

I also added the path variable to Intellij IDEA: click File -> Settings -> Path Variables and then add the variable in question (name of the program: X).

As @Jari-Jokinen mentioned above, for the IDE to use program 'X' from /path/to/X you need to restart your computer. Both Intellij IDEA and Android Studio are similar and developed by Jetbrains, so I hope this can be useful to someone.

Solution 19 - Android

Android Studio installs the latest Android SDK by default. Building a React Native app with native code, however, requires the Android 10 (Q) SDK in particular.

Solution 20 - Android

If you have a M1 Mac and are using Android Studio:

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSteve CView Question on Stackoverflow
Solution 1 - AndroidAlyView Answer on Stackoverflow
Solution 2 - AndroidJulio CBView Answer on Stackoverflow
Solution 3 - AndroidMostavView Answer on Stackoverflow
Solution 4 - AndroidChrisView Answer on Stackoverflow
Solution 5 - AndroidArishView Answer on Stackoverflow
Solution 6 - AndroidJari JokinenView Answer on Stackoverflow
Solution 7 - AndroidleoooooooView Answer on Stackoverflow
Solution 8 - AndroidpaulobungaView Answer on Stackoverflow
Solution 9 - AndroidAnaView Answer on Stackoverflow
Solution 10 - AndroidkenView Answer on Stackoverflow
Solution 11 - AndroidleizeQView Answer on Stackoverflow
Solution 12 - AndroidKamkoView Answer on Stackoverflow
Solution 13 - AndroidTaimoor Abdullah KhanView Answer on Stackoverflow
Solution 14 - Androidadamwong246View Answer on Stackoverflow
Solution 15 - AndroidNshpView Answer on Stackoverflow
Solution 16 - AndroidQasim ZubairView Answer on Stackoverflow
Solution 17 - Androidguru_codesView Answer on Stackoverflow
Solution 18 - AndroidintumwaView Answer on Stackoverflow
Solution 19 - AndroidAbdul RehmanView Answer on Stackoverflow
Solution 20 - AndroidPaulView Answer on Stackoverflow