反应原生在 Android 上: 无法运行程序“节点”: 错误 = 2,没有这样的文件或目录

编辑: 我不确定我采取了哪些步骤来解决这个问题,并且不再看到这个错误消息,所以我不能测试下面的解决方案。请单独尝试,因为不同的答案似乎对不同的人有效。

IOS 版本运行良好,但是在我的 Mac 上运行 Android Studio 我会得到下面的 Gradle Build 错误(转述) :

: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

我试过很多方法:

我曾经能够在终端运行纱线机器人,但即使这已经停止工作。

110227 次浏览

Android Studio 使用的 PATH 环境变量在 Android Studio 进程启动时是可用的,并且您的 Node 二进制目录应该位于该 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

只需将节点(以前由 nvm 安装)链接到 /usr/local/bin/node

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

Ubuntu 20.4,Node 14.15.0

我是怎么解决同样的问题的。

  1. 通过终端打开 Android 工作室
  2. 像以前一样构建项目

注意: 我通过 nvm 安装了 node (我相信这就是我遇到这个问题的原因)

如果你通过管理单元安装 android 工作室,那么通过命令行启动它是非常简单的。

在终端中打开项目文件夹并运行:

android-studio .

我在我的 Mac 上,我通过完全关闭 android 工作室来解决这个问题(关闭进程) ,然后重新启动它。

我在 M1迷你 Mac 上挣扎了很久。 我在 Intellij IDEA 打开 RN android 项目的时候,也是在尝试运行的时候得到了这个错误。命令行中的/gradlew 命令。

对我来说有一个解决办法:

  • Rm-rf. gradle (在 RN _ PROJECT/android 文件夹中)
  • Kill all java (停止运行 gradle 守护进程)
  • updated node to v16.8.0

我认为真正的解决办法是第一步。现在项目在 IDEA 中打开,没有错误,我可以运行了。格拉德鲁命令。

Android Studio 默认安装最新的 Android SDK。然而,使用本机代码构建 React Nativeapp 尤其需要 Android 10(Q) SDK。

这是因为 android 工作室无法找到节点,应该通过终端运行。

如果你正在使用 Mac,你可以在终端中使用这个命令运行 Android 工作室

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

2. 然后同步 Gradle

这样问题就解决了。

您可以关闭 gradle守护进程,或者只是重新启动您的机器,如果您做了一些更改,它将工作。

如果你有一台 M1 Mac 并且正在使用 Android Studio:

在我的这个设置中:

  • 苹果芯片 M1
  • Android Studio arctic fox 2020.3.1补丁2

我以前运行过上面提到的命令,但是一直出现同样的错误:

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

我不得不更新项目中的分级版本(Android/gradle/wrapper/gradle-wrapper. properties)

来自:

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

to:

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

并在项目中执行以下命令:

cd android && ./gradlew clean

或者直接删除这个文件夹,而不是执行前面的命令: 机器人/. gradle

做完这些步骤之后,我就不再有任何问题了。

在 android Studio 中,当这个问题发生在我身上时,我解决了这个问题:

1-转到文件-无效缓存/重新启动; 选择“无效并重新启动”选项。

对于 M1 Mac,我下载了 Android Studio (带苹果芯片的 Mac)版本,面临这个问题。

我把分级版本改成6.9-rc-1就修好了。

这可以从文件-> 项目结构-> 项目-> 梯度版本-> 类型或选择“6.9-rc-1”,并点击确定按钮。

您也可以在这里找到类似的和更多的解决方案。

级别同步失败: 原因: error = 2,没有这样的文件或目录

我有这个问题在 M1 Mac,找到了解决方案后3天,这是因为 nvm 和 .zsh有问题。如果你像我一样用 brew安装的话,会引起很多问题的。

解决方案:

我不得不 卸载

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

Then install nvm via instructions on 在它的 github 页面上的 readme

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

除此之外,还有一个关于 M1 Mac 故障排除的部分,如果需要的话可能会有进一步的帮助。但除此之外,Android Studio 现在运行良好,无需通过终端或任何其他解决方案打开它。(如果你的构建失败,你可能必须同意 Sdk 许可协议,查看该问题的构建选项卡,同意许可,只需进入 Tools > Sdk Manager 卸载 Sdk,单击 application 并重新打钩,Apply 重新安装它,它也会问你关于许可协议的问题。

许多答案只对了一半,但你必须同时做到以下两点:

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

  2. 从命令行启动 Android Studio。

解决方案

在终端运行 chmod +x /Applications/Android\ Studio.app/Contents/bin/printenv并重新启动 Android Studio 将使分级同步再次工作。

细节

我使用的是最新的 Android Studio,我对应用程序进行了补丁更新。我的系统在 M1 Pro Apple CPU 上运行 macOS 12.1。

重新启动后,我无法运行我的本机应用程序的反应和级别同步失败。上面的任何修正对我都没有帮助。因此,我详细阅读了 gradle 日志文件 idea.log。在日志的开头,我看到应用程序 printenv有一些问题。

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

之后,我进入文件夹/Applications/Android Studio.app/content/bin/,在我的终端中列出 printenv,发现 printenv没有执行权限:

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

安卓工作室

Android Studio Bumblebee | 2021.1.1 Build # AI-211.7628.21.2111.8092744,Build on January 19,2022构建 # AI-211.7628.21.21.2111.8092744,于2022年1月19日构建 运行时版本: 11.0.11.0-b60-7772763 aarch64 VM: JetBrains s.r.o 的 OpenJDK 64位服务器 VM。 MacOS 12.1 G1年轻一代,G1老一代 内存: 2048M 核心: 10 Registry: foreign al.system.auto.import.disked = true 注册表:

通过在 Mac 上的终端运行这个命令,我可以让它在安卓工作室的大黄蜂中工作,

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

然后重启机器人工作室。

you can use: 打开你的项目-> 运行:

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

在 M1的 Mac 上,升级 Android Studio 为我解决了这个问题。 我从北极狐升级到大黄蜂了。

也使用了这个版本的 Gradle (android/Gradle/wrapper/Gradle-wrapper. properties) :

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

我已经尝试了大部分的答案张贴在这里,但唯一的解决办法是升级到6.9或更高的等级。

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

等待它下载所有模块,它应该加载您的项目。

这不是这个问题的具体答案,但是这里的一些评论帮助我解决了我的问题,也许这个答案会帮助其他人。

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

我还将 Path 变量添加到 Intellij IDEA: 单击 File-> Settings-> Path Variables,然后添加有问题的变量(程序名称: 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.

在 Android Studio 中使缓存失效并在文件选项卡上重新启动。

加上我在蒙特雷的成功案例。

我使用这里的一个解决方案更新了网格版本。 这可以从文件-> 项目结构-> 项目-> 梯度版本-> 类型或选择“6.9-rc-1”,并点击确定按钮。

再次检查 node 是否安装了 node —— version,如果是的话 open -a /Applications/Android\ Studio.app 打开项目,错误就会消失。

为了我。

我用的是配有英特尔处理器的 Mac Pro 2019。

当我执行 yarn Android时,我得到了作者所说的同样的错误。

唯一有效的方法。

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

关于这个问题有很多解决方案,大多数都在讨论更新 gradle 插件。我测试了很多解决方案,并运行了最新的 Android Studio 版本(Chipmunk 2021.2.1 Patch 1)。

主要问题仍然是 AndroidStudio,其渐变版本低于6.9. x,无法正确查找 PATH 环境,这是由/bin/printenv 工具从 Android Studio 应用程序包本身设置的。如果您在 Android Studio 日志文件中看到它无法执行 printenv 命令,请更新到最新的 Android Studio 版本。

在“项目结构”中选择“分级版本”6.9或更高版本,您可以正确地构建您的项目。

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

你应该更新 Android Studio 到最新版本。它确实对我有用。

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

./gradlew --stop

你可以把你的 Android 工作室升级到2021.3.1。 对我们有用。

M1 Mac中有同样的问题,并且通过 terminal开设了机器人工作室
open -a /Applications/Android\ Studio.app

  1. 关闭机器人工作室
  2. 结束任务管理器的任何机器人任务
  3. 使用终端构建应用程序
cd android && ./gradlew build
  1. launch android studio
  2. 你应该能够在模拟器上运行你的应用程序

我在 Mac 上也遇到了问题(Monterey12.6.1)。我不知道确切的原因是什么,但我刚刚升级了我的 Android 工作室艺术狐到 Android Studio Dolphin | 2021.3.1补丁1。令人惊讶的是,我的问题解决了。