2020年以降、全く更新していなかった Node.js バージョン 10 でデプロイしていた Cloud Functions を Node.js バージョン 18 へ更新した際にやった作業まとめです。
ローカルテスト環境
macOS: 14.7.2 (23H311)
Node.js: 18.20.5
Firebase-tools: 13.28.0
作業内容
過去にデプロイした時のソースコードはありますが、macbook を買い換えたため、ローカルテスト環境から構築する必要がありました。
今回は、ローカルテスト環境の構築から Cloud Functions へのデプロイまでを書いていきます。
1. ローカル環境に Node.js と Firebase CLI を導入
a. Node.js
公式サイトに導入方法として、自動インストールスクリプト、スタンドアロンバイナリ、npm と3つ紹介されています。npm で前回も導入したので、npm を選択しました。
Firebase CLI リファレンス | Firebase ドキュメント
https://firebase.google.com/docs/cli?hl=ja#install-cli-mac-linux
Firebase CLI (firebase-tools) は、Node.js バージョン 18 以降が必要と Webサイトに注意書きがあったので、nvm を使用して、Node.js バージョン 18.20.5 を導入しました。導入手順は、以下の Web サイトを参考にしました。
[Node.js] nvmインストール手順(MacOS)
https://zenn.dev/nok_c7/articles/536ac2d35bf9e6
正しくインストールできたかは、node --version
で確認しました。
node --version
Node.js をインストールすると、npm コマンドツールが自動的にインストールされるので、Firebase CLI の導入に必要な npm の準備も完了です。
b. Firebase CLI
Firebase CLI は、公式サイトのガイダンスの通り、npm install -g firebase-tools
でインストールが可能で、firebase tools --version
でインストールされた firebase-tools のバージョンの確認ができます。
npm install -g firebase-tools
firebase tools --version
あとは、Firebase のアカウントへログインできるか試し、問題がなければ、導入完了です。
firebase login
firebase projects:list
私の環境では、特にトラブルもありませんでしたが、公式サイトには、エラーが発生した時の対処方法について記載があるので、エラーが発生する環境もあるようです。
2. Cloud Functions のプロジェクトを更新
a. npm でパッケージをインストール
npm install
私のプロジェクトでは、lockfile の Version が古いと警告が出ていましたが、npm install
により、package-lock.json にある lockfileVersion も同時に Version 3 へ更新され、警告が解消しました。
b. Node.js のバージョンを更新
package.json に記載している engines を更新し、Node.js のバージョンを 10 から 18 へ変更します。
"node": "10" -> "node": "18"
c. index.js を修正
index.js で Functions のバージョンを v1 に指定します。
const functions = require('firebase-functions')
->
const functions = require('firebase-functions/v1')
以前、構築した時は Functions の v2 が出る前だったのか、バージョンを指定しなくても良かった気がします。ここでバージョンを指定しないと v2 扱いとなるようで、後述の firebase deploy
でエラーが発生しました。
v2 へアップグレードしたい場合には、手順が公式サイトで公開されていますが、今回は v1 でいいかなと思っています。
d. パッケージの更新
firebase 関連のパッケージを更新します。他に axios を使用しているので、ここで合わせて更新しました。脆弱性の警告が出ていたので npm audit fix
も実行しておきます。
npm install firebase-functions@latest firebase-admin@latest axios@latest
npm install firebase-functions-test@latest --save-dev
npm audit fix
e. Functions の初期化
パッケージの更新後にエミュレーターによりテストをしようとしたらエラーが出てしまいました。なんらかの情報が欠落しているのかとは思いましたが、4年前の記憶はないので、再度、firebase init functions
で初期化することで対処しました。
firebase init functions
実行すると以下のような質問がされるので、データを上書きされないようにほとんど No を選択しました。
? What language would you like to use to write Cloud Functions? JavaScript
? Do you want to use ESLint to catch probable bugs and enforce style? No
? File functions/package.json already exists. Overwrite? No
i Skipping write of functions/package.json
? File functions/index.js already exists. Overwrite? No
i Skipping write of functions/index.js
? File functions/.gitignore already exists. Overwrite? Yes
✔ Wrote functions/.gitignore
? Do you want to install dependencies with npm now? Yes
3. ローカルテスト環境を構築
エミュレーターの初期化して、ローカルテストができるようにしていきます。
firebase init emulators
Emulator UI を使用したことがないので、必要性がわかりませんでしたが、特にあっても問題はないと思ったので、Yes で進んでいます。
=== Emulators Setup
? Which Firebase emulators do you want to set up? Press Space to select emulators, then Enter to confirm your choices. Functions Emulator
? Which port do you want to use for the functions emulator? 5001
? Would you like to enable the Emulator UI? Yes
? Which port do you want to use for the Emulator UI (leave empty to use any available port)?
? Would you like to download the emulators now? Yes
i Writing configuration info to firebase.json...
i Writing project information to .firebaserc...
✔ Firebase initialization complete!
初期化が完了したらエミュレータを起動して、各 function が、問題なく動作しているか確認しました。
firebase emulators:start --only functions
4. Cloud Functions へデプロイ
テストが終われば、あとはデプロイするだけなので、これで終わりです。
firebase deploy --only functions
さいごに
ずっとやらないといけないなと思ってやれずにいましたが、Node.js バージョン 10 は、さすがに古いので年末の大掃除の気分でやり切りました。すぐに バージョン 18 も古くなるような...