Steve C
Steve C

Reputation: 1896

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

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.

Upvotes: 184

Views: 204595

Answers (30)

Mostav
Mostav

Reputation: 2612

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

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

Can be tested by opening the Android Studio built-in terminal and running node --version. Must be re-run every time the node version is updated by nvm use.

Environment: Ubuntu 20.4, Node 14.15.0. Also MacOS Sequoia, Android Studio Ladybug.

Upvotes: 114

cdeutsch
cdeutsch

Reputation: 3952

I got this error in an Expo app after I upgraded Android Studio.

Restarting the entire Mac fixed it.

Upvotes: 0

Colby McHenry
Colby McHenry

Reputation: 51

If you are running MacOS + NVM (Node Version Management) + M1, M2, M3 + Homebrew etc. this is what fixed the issue for me.

You need to STOP ALL GRADLE DAEMONS. With command:

./gradlew --stop

Then open Android Studio from your Terminal with this command and NOT THE DOCK

open -a /Applications/Android\ Studio.app

Upvotes: 5

Jiacheng Deng
Jiacheng Deng

Reputation: 1

I ran into the same issue on Mac. I tried to open Android Studio from the terminal and I also tried to reinstall it. None of them could fix the issue. Finally, I switched the Gradle JDK version through settings -> Build, Execution, Deployment -> Build Tools -> Gradle. I switched it to the openjdk17 that I installed by myself, and the problem was solved.

Upvotes: 0

Silicon Socket
Silicon Socket

Reputation: 221

I had the same issue on React Native, with newArch enabled, I tried a lot of solutions, but finally this worked for me:

I added the node path into the file android/app/build.gradle setting the nodeExecutableAndArgs property like this:

import groovy.json.JsonSlurper

project.ext.react = [
    nodeExecutableAndArgs: [getNodePath()]
]

def getNodePath() {
    def process = "which node".execute()
    process.waitFor()
    def nodePath = process.in.text.trim()
    if (!nodePath) {
        throw new GradleException("Node.js executable not found in PATH. Please ensure Node.js is installed.")
    }
    return nodePath
}

This solution ensures that Gradle dynamically retrieves the correct path to node during the build process.

Upvotes: 0

Muhammad Haris Baig
Muhammad Haris Baig

Reputation: 106

change the gradle settings to use JAVA_HOME instead of GRADLE_LOCAL_JAVA_HOME or any other gradle jdk

Upvotes: 0

jcdhlzq
jcdhlzq

Reputation: 11

Change the default JDK version from Android studio embeded JDK11 to JDK zulu version. That works for me on Apple M2. This changed the gradle scripts running env PATH, after which node can be found. But I don't know why this change affect env.

Upvotes: 1

Aly
Aly

Reputation: 5041

It's because Android Studio couldn't find node and should be run from a terminal window.

If you are using Mac you can run Android Studio using this command in terminal. Note that if Android Studio is already running, it will need to be shut down completely before running the command.

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

If you installed Android Studio through Jetbrains Toolbox version 1.x, run

open -a ~/Applications/JetBrains\ Toolbox/Android\ Studio.app

If you installed Android Studio through Jetbrains Toolbox version 2.x, run

open "Applications/Android Studio.app"

2. Then sync Gradle

And this will solve the problem.

Note: If you are using Node through nvm, you should run nvm use [version] in the same terminal window before running the above command to open Android Studio.

Upvotes: 428

Nothing helped me, just when I changed Gradle JDk it started working

Upvotes: 7

Kris B.
Kris B.

Reputation: 63

If you're using nvm, this can work: https://stackoverflow.com/a/69066155/10047450

But you need to run nvm use [version] (or just nvm use if you have an .nvmrc configured) in the terminal before launching android studio.

Upvotes: 0

Hussnain Hashmi
Hussnain Hashmi

Reputation: 223

I was also facing issue on Mac(Monterey 12.6.1).I don't know what was the exact reason but I've just upgraded my Android studio artic fox to Android Studio Dolphin | 2021.3.1 Patch 1. And surprisingly my issue is resolved.

Upvotes: 0

awaleed
awaleed

Reputation: 123

  1. close android studio
  2. end any android task from the task manager
  3. use terminal to build the app using
cd android && ./gradlew build
  1. launch android studio
  2. you should be able to run your app on a simulator

Upvotes: 1

sandulasanth-7
sandulasanth-7

Reputation: 388

Had the same issue in m1 mac and opening the android studio via terminal worked for me
open -a /Applications/Android\ Studio.app

Upvotes: 21

pramod J B
pramod J B

Reputation: 77

You can upgrade your Android studio to 2021.3.1. Its working for us.

Upvotes: 0

friederbluemle
friederbluemle

Reputation: 37203

I just ran into this issue as well, and for me the solution was to stop the running Gradle daemon:

./gradlew --stop

Upvotes: 19

Bao Tran
Bao Tran

Reputation: 174

You should update Android Studio to latest version. It does work for me.

Upvotes: 0

Chris
Chris

Reputation: 767

There are a lot of solutions about this issue and most are talking about updating the gradle plugin. I have tested a lot of solutions and running the latest Android Studio version (Chipmunk 2021.2.1 Patch 1).

The main issue is still that AndroidStudio, with gradle version lower than 6.9.x, can not lookup properly the PATH environment, which is set by /bin/printenv tool from Android Studio application package itself. If you see in the Android Studio logfile that it can not execute the printenv command, please update to latest Android Studio version.

In 'Project Structure' select 'Gradle Version' 6.9 or higher and you can build your project properly.

In 'Project Structure' select 'Gradle Version' 6.9 or higher

Upvotes: 0

Jackson Smith
Jackson Smith

Reputation: 716

For me.

I'm using a Mac Pro 2019 with Intel Processor.

when I executed yarn Android I got that same error the author said.

The only thing that really works.

Looks like that, in my case, some Android Studio config was wrong.

Upvotes: 0

Bobby Nuel
Bobby Nuel

Reputation: 41

Adding what worked for me on MacOs Monterey.

I updated the graddle version using one of the solutions here. This can be done from File -> Project Structure -> Project -> Gradle Version -> Type or select "6.9-rc-1" and click OK button.

Double-check if node is installed with node --version, if it is open -a /Applications/Android\ Studio.app Open the project and the error should disappear.

Upvotes: 2

A. Berk
A. Berk

Reputation: 305

Invalidate Caches & Restart on File Tab in Android Studio.

Upvotes: 7

intumwa
intumwa

Reputation: 354

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.

Upvotes: 0

Jari Jokinen
Jari Jokinen

Reputation: 770

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

Upvotes: 25

guru_codes
guru_codes

Reputation: 41

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.

Upvotes: 3

ken
ken

Reputation: 4077

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

Upvotes: 9

Nshp
Nshp

Reputation: 21

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

Upvotes: 2

Chris
Chris

Reputation: 767

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

Upvotes: 61

Qasim Zubair
Qasim Zubair

Reputation: 197

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.

Upvotes: 2

adamwong246
adamwong246

Reputation: 795

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.

Upvotes: 1

Taimoor Abdullah Khan
Taimoor Abdullah Khan

Reputation: 324

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.

Upvotes: 0

Arish
Arish

Reputation: 410

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.

Gradle sync failed: Cause: error=2, No such file or directory

Upvotes: 40

Related Questions