You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Firebase Functions默认ESLint报Unexpected token =>错误解决方法

问题现象

2022版Firebase Functions云函数开发过程中,执行预部署阶段的ESLint校验时报错,终端输出如下:

error  Parsing error: Unexpected token =>

✖ 1 problem (1 error, 0 warnings)


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

涉及的项目配置与代码如下:

  • 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
}
  • .eslintrc.js配置
module.exports = {
  root: true,
  env: {
    es6: true,
    node: true,
  },
  extends: [
    "eslint:recommended",
    "google",
  ],
  rules: {
    quotes: ["error", "double"],
  },
};
  • 脱敏后index.js代码
const admin = require('firebase-admin')
admin.initializeApp()

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

核心疑问点:

  • 如何正确升级ESLint版本
  • 是否需要安装babel-eslint(或其更新版本的替代包)作为解析依赖
  • 是否需要配置启用ES7或ES8版本规则,以及对应的具体配置操作方法
解决方案

1. ESLint升级操作

当前项目依赖的ESLint 8.9.0已原生支持async箭头函数语法,无强制升级必要。如果需要升级到8.x分支的最新稳定兼容版本,进入functions目录执行以下命令即可:

npm install --save-dev eslint@8

不建议升级到ESLint 9.x版本,该版本默认使用扁平配置格式,和现有项目的.eslintrc.js配置不兼容,会触发新的配置报错。

2. babel-eslint依赖安装判断

不需要安装babel-eslint(已更名为@babel/eslint-parser)。项目中使用的async箭头函数是Node.js 16原生支持的标准ECMAScript语法,ESLint自带的默认解析器Espree完全可以识别,额外引入Babel解析依赖只会增加项目复杂度和依赖体积,没有实际收益。

3. ES版本配置操作(核心修复项)

本次报错的根因是现有ESLint配置仅开启了ES6(ES2015)语法支持,而async异步函数是ES2017(ES8)的标准特性,解析器遇到未开启的高版本语法时就会抛出Unexpected token =>错误。
按以下方式修改.eslintrc.js配置即可修复:

module.exports = {
  root: true,
  env: {
    es2021: true, // 替换原es6: true配置,覆盖ES2021及之前所有标准语法,完全兼容Node.js 16运行时
    node: true,
  },
  parserOptions: {
    ecmaVersion: 2021, // 显式指定解析器支持的ECMAScript版本,和env配置保持对齐
  },
  extends: [
    "eslint:recommended",
    "google",
  ],
  rules: {
    quotes: ["error", "double"],
  },
};

额外注意:现有index.js代码未引入firebase-functions依赖,会导致函数运行时报错,需要在文件顶部补充以下语句:

const functions = require('firebase-functions');

配置修改完成后,先本地执行npm run lint验证无报错,再执行部署操作即可。

内容的提问来源于stack exchange,提问作者singing

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.01 00:21:40