Firebase Emulator が起動しないエラーの解決方法
ローカル環境でFirebaseのElulatorを試してたが詰まったので、解決した方法を記事にします。
Firebase Emulator Suite を起動しようとした際に、以下のエラーが発生しました。
実際のエラー
$firebase emulators:start
i emulators: Starting emulators: functions, hosting
⚠ hosting: Hosting Emulator unable to start on port 5000, starting on 5002 instead.
⚠ functions: The following emulators are not running, calls to these services from the Functions emulator will affect production: auth, firestore, database, pubsub, storage, dataconnect
i hosting[xxx]: Serving hosting files from: .output/public
✔ hosting[xxx]: Local server: http://127.0.0.1:5002
i ui: Emulator UI logging to ui-debug.log
⚠ ui: Fatal error occurred:
Emulator UI has exited with code: 1,
stopping all running emulators
エラー詳細 (ui-debug.log の内容)
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/user/.cache/firebase/emulators/ui-v1.13.0/server/server.mjs not supported.
Instead change the require of /xxx/xxx/.cache/firebase/emulators/ui-v1.13.0/server/server.mjs to a dynamic import() which is available in all CommonJS modules.
解決方法
以下の手順でエラーが解消され、正常にFirebase Emulatorを起動できました。
- Firebase CLI をアンインストール
まず、現在インストールされている Firebase CLI を削除します。
npm uninstall -g firebase-tools
- npm キャッシュをクリア
古いキャッシュが影響している可能性があるため、npm のキャッシュを削除します。
npm cache clean --force
- Firebase CLI を再インストール
Firebase CLI を再インストールします。
npm install -g firebase-tools
- Firebase に再ログイン
再インストール後、Firebase にログインし直します。
firebase login
- Firebase Emulator を起動
再度、エミュレーターを起動します。
firebase emulators:start
正常に Firebase Emulator が起動しました!
firebase-toolのバーションは以下です。
firebase-tools@13.34.0
考察
古いFirebase EmulatorがNode.jsのrequire()を使えなかったのだと思います。
新しいFirebase CLIでは修正されている可能性があるため、firebase-toolsを再インストールすることで解決 しました。