ReactNativeで新規プロジェクト作成後に起動が失敗する場合

ReactNative

ReactNativeで新規プロジェクトを作成した後、Androidでサンプルプログラムが実行できなく、危うくはまりかけたので解決策をまとめておきます。

エラー内容

サンプルプログラムを実行しようとすると、以下のようなエラーがでました。

PS ~est1> npx react-native run-android 
* daemon not running; starting now at tcp:5037
* daemon started successfully
info Installing the app...
Starting a Gradle Daemon, 1 incompatible and 2 stopped Daemons could not be reused, use --status for details

info 💡 Tip: Make sure that you have set up your development environment correctly, by running npx react-native doctor. To read more about doctor command visit: https://github.com/react-native-community/cli/blob/main/packages/cli-doctor/README.md#doctor 


FAILURE: Build failed with an exception.

* Where:
Settings file '~test1\android\settings.gradle' line: 2

* What went wrong:
Error resolving plugin [id: 'com.facebook.react.settings']
> java.io.UncheckedIOException: Could not move temporary workspace ~

原因と解決策

* What went wrong:
Error resolving plugin [id: 'com.facebook.react.settings']
> java.io.UncheckedIOException: Could not move temporary workspace ~

このエラー内容から、temporary workspaceを移動できなかったのが読み取れました。

WindowsのPowerShellではスクリプト実行時のポリシーによってできることが変わります。
(危ないスクリプトをむやみに実行されないようにするためなどですね。)

一応、上記推論が正しいか、PowerShellの実行ポリシーを確認します。

PS~>Get-ExecutionPolicy
Restricted

「Restricted」と表示された場合、厳格なポリシーとなっているため、ディレクトリの移動などで制限が掛かっていた形ですね。

それでは、こちらを「RemoteSigned」に変更します。
※RemoteSignedが妥当かどうかはお使いのPC・作業環境に依存しますのでご精査ください。(あくまで自己責任です。)
(企業のPCの場合はセキュリティが甘くなる/そもそも設定できない 等ありますので注意が必要です。)

管理者権限でPowerShellを開き、以下を実行します。

PS~>Set-ExecutionPolicy RemoteSigned

再び「Get-ExecutionPolicy」を実行し、「RemoteSigned」と表示されていれば完了です。

今回は以上です。