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

Firebase Functions部署报错Parsing error: Unexpected token => 问题求助

问题原因
  • 语法解析规则不兼容:项目的ESLint配置默认ECMAScript版本过低,无法识别async箭头函数语法,就会抛出Parsing error: Unexpected token =>错误。
  • Node.js runtime版本过低:Firebase Functions如果没有指定高版本Node.js运行时,低于8.3的旧版本原生不支持async/await语法。
  • 代码存在未定义变量:返回值里的approved、risk.flagged两个变量没有从返回的payment对象中取值,直接引用会导致运行时报错。
解决方案

步骤1:调整ESLint配置

找到项目functions目录下的.eslintrc.js文件,修改parserOptions的ecmaVersion为2017及以上:

module.exports = {
  parserOptions: {
    ecmaVersion: 2017, // 至少设为2017,支持async/await语法
  },
  // 其他原有配置保留
}

步骤2:指定Node.js运行时版本

打开functions目录下的package.json文件,新增engines字段指定Node.js 18 LTS版本(Firebase当前支持的稳定版本):

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "engines": {
    "node": "18"
  },
  // 其他原有配置保留
}

步骤3:修正代码中的未定义变量

调整return语句的取值路径,从payment对象中获取对应字段:

const functions = require("firebase-functions");
const { Checkout } = require("checkout-sdk-node");
const cko = new Checkout("sk_test_XXXXX-XXXX-XXXX");

exports.payWithToken = functions.https.onCall(async (data, context) => {
    try {
        const payment = await cko.payments.request({
            source: {
                token: data.token,
            },
            customer: {
                email: context.auth.token.email
            },
            currency: data.currency,
            amount: data.amount,
        })
        return {
            "Status": payment.status,
            "3DS-Link": payment.redirectLink,
            "Approved": payment.approved, // 从payment对象取值
            "Flagged": payment.risk?.flagged, // 可选链避免risk字段为空时报错
        };
    } catch (error) {
        console.log(error)
        throw new functions.https.HttpsError(error.name, error.message, error);
    }
});

修改完成后重新执行部署命令firebase deploy --only functions即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 18:00:03