ちょっと困った症状
- Firebase の functions で
async/await
を使ったらデプロイが error になってしまう - ローカルで
firebase functions:shell
で、直接関数を動かすと問題ない(ローカルでは問題ない)
原因と対策
うまくいかない原因は二つ。
原因1 - デプロイ時にESLintが怒ってる
エラーはこんな感じ
async
の入ってるところでエラーになる
61:9 error Parsing error: Unexpected token function
✖ 1 problem (1 error, 0 warnings)
対策1
.eslintrc.json
を以下のように"ecmaVersion": 2017
とする。Firebaseがつくるデフォルト設定 "ecmaVersion": 6
だと、ESLint
はasync/await
を解釈しない模様
"parserOptions": {
// Required for certain syntax usages
//"ecmaVersion": 6
"ecmaVersion": 2017
},
原因2 - node
のバージョンが async/await
に対応していない
エラーはこんな感じ
async
の入ってるところでエラーになる
⚠ functions[getSentiment2(us-central1)]: Deployment error.
Function failed on loading user code. Error message: Code in file index.js can't be loaded.
Is there a syntax error in your code?
Detailed stack trace: /user_code/index.js:61
async function quickstart() {
^^^^^^^^
対策2
packeges.json
に "engines": { "node": "8" }
を追加して、node
のバージョン8を指定する。FireBaseの node
ってデフォルトが6
らしいのですが、8
以降じゃないと async/await
がつかえないみたいです。この状態でデプロイすると
i functions: updating Node.js 8 function func1(your-region)...
i functions: updating Node.js 8 function func2(your-region)...
と表示されて、デプロイできました。
参考にさせていただきました
おわりに
誰かの時間の節約になれば。
Cheers,