LoginSignup
3

More than 3 years have passed since last update.

Firebase の functions で async/await しようと思ったら怒られた件

Last updated at Posted at 2019-04-13

ちょっと困った症状

  • 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 だと、ESLintasync/awaitを解釈しない模様

  "parserOptions": {
    // Required for certain syntax usages
    //"ecmaVersion": 6
    "ecmaVersion": 2017
  },

関連するstackoverflowのQ&A

原因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)...

と表示されて、デプロイできました。

関連するstackoverflowのQ&A

参考にさせていただきました

おわりに

誰かの時間の節約になれば。

Cheers,

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3