React Native Android: Cannot run program "node" error
When working with React Native on Android, developers frequently encounter the error: Cannot run program "node": error=2, No such file or directory
. This occurs when Gradle cannot locate the Node.js executable during the build process.
Problem Overview
The error typically appears in the Gradle build output:
:ReactNative:Running '[node, -e, console.log(require('react-native/cli').bin);]' command failed.
- Where:
Script '/Users/.../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
This issue commonly affects developers using:
- macOS environments (especially M1/M2/M3 chips)
- Node.js version managers (nvm, n)
- Recent Android Studio versions
Solutions
1. Launch Android Studio from Terminal
The most reliable solution is to start Android Studio from your terminal:
# Standard installation
open -a /Applications/Android\ Studio.app
# JetBrains Toolbox v1.x
open -a ~/Applications/JetBrains\ Toolbox/Android\ Studio.app
# JetBrains Toolbox v2.x
open "Applications/Android Studio.app"
WARNING
Ensure Android Studio is completely closed before running this command.
2. NVM Users: Set Node Version
If using nvm, set your Node.js version before launching Android Studio:
# Set specific version
nvm use 18.0.0
# Or use version from .nvmrc
nvm use
# Then launch Android Studio
open -a /Applications/Android\ Studio.app
3. Create Symbolic Link for Node
Create a symlink to make node available system-wide:
sudo ln -s "$(which node)" /usr/local/bin/node
TIP
This needs to be recreated if you change Node versions with nvm.
4. Stop Gradle Daemons
Sometimes stopping existing Gradle processes resolves the issue:
# In your android directory
./gradlew --stop
5. Update Gradle Configuration
Add this to your android/app/build.gradle
to dynamically locate Node:
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
}
6. Update Android Studio and Gradle
Ensure you're using compatible versions:
Update Android Studio to the latest version
Use Gradle 6.9 or newer in
android/gradle/wrapper/gradle-wrapper.properties
:propertiesdistributionUrl=https\://services.gradle.org/distributions/gradle-6.9-bin.zip
7. Fix File Permissions (macOS)
On macOS, sometimes Android Studio's printenv
tool loses execute permissions:
sudo chmod +x /Applications/Android\ Studio.app/Contents/bin/printenv
8. Change Gradle JDK Settings
In Android Studio:
- Go to File → Settings → Build, Execution, Deployment → Build Tools → Gradle
- Change Gradle JDK to use your installed JDK (not the embedded one)
9. Restart and Rebuild
Sometimes simple solutions work:
# Restart computer
sudo shutdown -r now
# Or clean and rebuild
cd android && ./gradlew clean
cd .. && npx react-native run-android
Environment Configuration Best Practices
To prevent this issue:
- Install Node.js properly: Use official installers or ensure nvm installations are complete
- Verify PATH: Ensure your Node.js path is in your shell startup files (.bashrc, .zshrc)
- Keep tools updated: Regularly update Android Studio, Gradle, and Node.js
- Use consistent environments: Consider using Docker or environment managers for consistent development setups
Troubleshooting Steps
If the issue persists:
- Check Node.js is installed:
node --version
- Verify Android Studio can access Node by checking the built-in terminal
- Examine the full Gradle logs with
--info
or--debug
flags - Check Android Studio's idea.log for environment issues
INFO
The React Native CLI uses Node.js to execute various build tasks, which is why proper Node.js configuration is essential for Android builds.
By following these solutions, most developers can resolve the "Cannot run program node" error and successfully build their React Native Android applications.