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

为何无法部署Flutter项目中的JavaScript版Firebase Cloud Functions?

解决Firebase Cloud Functions部署因ESLint语法错误失败的问题

问题背景

我在Flutter项目中实现推送通知功能,希望Firebase Cloud Functions在Firestore的rooms集合文档变更时触发通知,使用Node.js 16编写JavaScript函数。部署时遇到ESLint语法错误导致失败。

复现步骤

  • 执行firebase init functions
  • 选择“Use an existing project”并选中项目my-project-1
  • 选择JavaScript作为开发语言,启用ESLint
  • 在index.js中编写以下代码:
const functions = require('firebase-functions');
const admin = require('firebase-admin');

admin.initializeApp();

// fcm notification
exports.sendNotification = functions.firestore
  .document("rooms/{roomId}/")
  .onWrite(async(change, context) => {

    const payload = {
      "notification":{
        "title": "Firebase messaging",
        "body": "A message",
      },
    };
    const options = {};
    return admin.messaging().sendToDevice('TOKEN-ID', payload, options);
  });
  • 执行npm run deploy,返回错误:
9:34  error  Parsing error: Unexpected token =>

✖ 1 problem (1 error, 0 warnings)

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

对应的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": {
    "firebase-admin": "^10.0.2",
    "firebase-functions": "^3.18.0"
  },
  "devDependencies": {
    "eslint": "^8.9.0",
    "eslint-config-google": "^0.14.0",
    "firebase-functions-test": "^0.2.0"
  },
  "private": true
}

错误原因

你使用的eslint-config-google默认配置未适配Node.js 16支持的异步箭头函数语法,导致ESLint识别async =>时抛出解析错误。

解决方法

方案1:修改ESLint配置适配Node.js 16语法

在functions目录下创建或修改.eslintrc.js文件,添加Node.js环境配置并指定ECMAScript版本:

module.exports = {
  env: {
    es2021: true,
    node: true,
  },
  extends: 'google',
  parserOptions: {
    ecmaVersion: 'latest',
  },
  rules: {
    // 可选:根据代码风格调整规则,比如缩进、引号类型
    'indent': ['error', 2],
    'quotes': ['error', 'single'],
  },
};

方案2:跳过ESLint检查直接部署

如果不需要ESLint验证,修改package.json中的deploy命令,跳过predeploy的lint步骤:

"scripts": {
  "lint": "eslint .",
  "serve": "firebase emulators:start --only functions",
  "shell": "firebase functions:shell",
  "start": "npm run shell",
  "deploy": "firebase deploy --only functions --no-predeploy",
  "logs": "firebase functions:log"
},

修改后重新执行npm run deploy即可。

额外建议

代码中document("rooms/{roomId}/")末尾多了一个斜杠,建议改为document("rooms/{roomId}"),避免不必要的路径匹配问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 10:37:31