react-native android 环境报错 node , Could not start 'node',error=2, No such file or director


1. android 环境下报错

   Could not start 'node' 或者 error=2, No such file or directory 

  A problem occurred starting process 'command 'node''

错误信息 : 

我的项目构建失败,因为它使用了不正确的目录来运行 Node.js。如何为这些编译时任务设置节点目录?

具体任务是:

应用程序:recordFilesBeforeBundleCommandDebug

以及相关的错误:

org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:recordFilesBeforeBundleCommandDebug'.
Caused by: org.gradle.process.internal.ExecException: A problem occurred starting process 'command 'node''
Caused by: net.rubygrapefruit.platform.NativeException: Could not start 'node'
Caused by: java.io.IOException: Cannot run program "node" (in directory "/Users/me/Code/appname/android/app"): error=2, No such file or directory
Caused by: java.io.IOException: error=2, No such file or directory

  解决办法:

    在 项目/android/app/build.gradle 文件添加下面代码

      第一种:添加-> nodeExecutableAndArgs: ["/usr/local/bin/node"]

project.ext.react = [ entryFile: "index.js", nodeExecutableAndArgs: ["/usr/local/bin/node"] ]   第二种: 添加下面代码
ext {    
    react = [
        nodeExecutableAndArgs:  ["/usr/local/bin/node"]
    ]
}

注意: 上面 /usr/local/bin/node 是自己本地启动【安装】node的路径,mac系统可以在终端输入 which node 命令 来得到node的路径

   

   在rn 新的版本中上面方法无效,可以试一下下面的方法

    1.打开终端,运行这个 open -a /Applications/Android\ Studio.app

    2.然后同步Gradle

    其他方法请参考:https://stackoverflow.com/questions/61922174/react-native-on-android-cannot-run-program-node-error-2-no-such-file-or-dir