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