Quantcast
Viewing all articles
Browse latest Browse all 8875

Cloud Functions を Node.js 10 移行する

久しぶりに Cloud Functions のコンソール画面を開いたら下記のような警告が来ていました。どうやら Node.js 8 が非推奨になり2021年の3月中旬には全面的にサポートされなくなるとのことなので、Node.js 10 へ移行します。

Image may be NSFW.
Clik here to view.
image.png

https://cloud.google.com/functions/docs/migrating/nodejs-runtimes?hl=ja

移行していく

まずは Node.js 10 にバージョンアップするので、Deploy・エミュレーションができるように最新の Firebase CLI をインストールします。

$ npm install -g firebase-tools@latest

次に functions> package.jsonenginesブロックの nodeターゲットを 10 に変更します。

"engines":{"node":"10"}

ほとんどの場合 Node.js 8 と Node.js 10 ではコードが変更されることはありませんが、自動的にセットされる環境変数が変更されたみたいなので、詳しくは下記リンクを参照してください。

https://cloud.google.com/functions/docs/env-var#environment_variables_set_automatically

テストしてみる

firebase emulators:startでエミュレートをスタートさせ、View in Emulator UIで表示されたローカルポートで Emulator Suite を開きます。

Image may be NSFW.
Clik here to view.
image.png

emulator がインストールされていない場合は下記コマンドでインストールします。

$ npm install -g @google-cloud/functions-emulator

http://localhost:5001/{project_name}/us-central1/{関数名}のように HTTP関数でリクエストを実行すれば、ローカル環境でテストを行うことができるようになります。

https://firebase.google.com/docs/functions/get-started?hl=ja#emulate-execution-of-your-functions

テストが完了したので最後にデプロイします。

$ firebase deploy --only functions

Viewing all articles
Browse latest Browse all 8875

Trending Articles