Home > other >  Unexpected token => on async using Firebase Functions (2022) default ESlint
Unexpected token => on async using Firebase Functions (2022) default ESlint

Time:06-08

My package.json

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "lint": "eslint .",
    "serve": "firebase emulators:start --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "16"
  },
  "main": "index.js",
  "dependencies": {
    "axios": "^0.27.2",
    "firebase-admin": "^10.0.2",
    "firebase-functions": "^3.21.2"
  },
  "devDependencies": {
    "eslint": "^8.9.0",
    "eslint-config-google": "^0.14.0",
    "firebase-functions-test": "^0.2.0"
  },
  "private": true
}

My .eslintrc.js

module.exports = {
  root: true,
  env: {
    es6: true,
    node: true,
  },
  extends: [
    "eslint:recommended",
    "google",
  ],
  rules: {
    quotes: ["error", "double"],
  },
};

A scrubbed version of my index.js

const admin = require('firebase-admin')
admin.initializeApp()

exports.sendMessage = functions.https.onCall(async (input) => {
    // my code here
})

Error from terminal

error  Parsing error: Unexpected token =>

✖ 1 problem (1 error, 0 warnings)


Error: functions predeploy error: Command terminated with non-zero exit code1

I'm thinking that the ESLint version that is being applied may be out of date? If so, how do I update it? Do I need to use a babel-eslint package (or newer)? Do I need to apply ES7 or 8 rules instead, and how?

CodePudding user response:

You've only enabled es6: true. However, async functions are part of ES8 (ES2017). You'll want to use something more modern than ES6. Alternatively, set the parser options specifically.

CodePudding user response:

I updated my .eslintrc.js file

module.exports = {
  root: true,
  parserOptions: {
    ecmaVersion: "latest",
  },
  env: {
    node: true,
  },
  extends: [
    "eslint:recommended",
    "google",
  ],
  rules: {
    quotes: ["error", "double"],
  },
};

This way, es6 was removed as a true value in env, and parserOptions was added with ecmaVersion set to latest release. I then just applied the rules based on the failed items. It works, but was a bit of a hassle. I suppose the code is cleaner at least...

  • Related