【问题标题】:*ESLint Error* in React when trying to deploy Firebase Functions尝试部署 Firebase 函数时 React 中出现 *ESLint 错误*
【发布时间】:2021-06-04 14:44:46
【问题描述】:

使用 Firebase 函数设置无服务器后端。我看过的设置 Functions 文件夹的教程都推荐使用 ESLint 功能来捕获可能的错误并强制执行样式。 POST lambda 路由在本地部署时通知解析错误,但一切仍按需要工作。我去将后端部署到 Firebase 并抛出了一堆错误 - 因此不让我继续。我转到它说我有错误的行,删除异步等待,错误消失但代码中断。我究竟做错了什么?是否还有一种方法可以部署我的代码,而无需删除 Functions 文件夹并重新进行操作?

functions 文件夹中的

index.js 文件:

const functions = require("firebase-functions");
const express = require("express");
const cors = require("cors");
const dotenv = require("dotenv");

dotenv.config();

const stripe = require("stripe")(`${process.env.REACT_APP_STRIPE_SECRET_KEY}`);

// App config
const app = express();

// Middlewares
app.use(cors({origin: true}));
app.use(express.urlencoded({extended: true, useNewUrlParser: true}));
app.use(express.json());

// API routes
app.get("/", (req, res) => res.status(200).send("Hello World"));

// ???? PARSING ERROR PREVENTING DEPLOY ???? 
app.post("/payments/create", async (req, res) => {
  const total = req.query.total;

  console.log("Payment Request Received for: , total");

  const paymentIntent = await stripe.paymentIntents.create({
    amount: total, //sub-units of currency
    currency: "USD",
  });

  res.status(201).send({
    clientSecret: paymentIntent.client_secret,
  })
});

// Listen command
exports.api = functions.https.onRequest(app);
functions 文件夹中的

.eslintrc.js 文件:

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

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": "12"
  },
  "main": "index.js",
  "dependencies": {
    "cors": "^2.8.5",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "firebase-admin": "^9.2.0",
    "firebase-functions": "^3.11.0",
    "stripe": "^8.137.0"
  },
  "devDependencies": {
    "eslint": "^7.6.0",
    "eslint-config-google": "^0.14.0",
    "firebase-functions-test": "^0.2.0"
  },
  "private": true
}

尝试部署时终端出错

i  deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint

> functions@ lint /Users/Desktop/coding-repos/ecommerce/client/functions
> eslint .


/Users/Desktop/coding-repos/ecommerce/client/functions/index.js
  22:47  error  Parsing error: Unexpected token =>

✖ 1 problem (1 error, 0 warnings)

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! functions@ lint: `eslint .`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the functions@ lint script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/.npm/_logs/2021-03-05T22_26_21_954Z-debug.log

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

【问题讨论】:

  • 刚开始使用云功能时遇到了和你一样的问题。我个人在设置时禁用了 ESLint,但我不建议这样做。这是一个与您的问题非常相似的已回答问题,可以帮助您找到可行的解决方案。 stackoverflow.com/questions/48602833/…

标签: node.js firebase google-cloud-functions eslint serverless


【解决方案1】:

在这些情况下,最好阅读堆栈跟踪。错误消息指出问题或可能是您可以调试的问题。

如您所见,错误位置在您的index.js 文件中的22:47 附近。尽管这并不总是准确的,但您可以假设错误将在该行上或可能在该行之上或之下。这也有例外。例如,您可能在代码的其他地方没有右括号。

/Users/Desktop/coding-repos/ecommerce/client/functions/index.js
  22:47  error  Parsing error: Unexpected token =>

✖ 1 problem (1 error, 0 warnings)

此外,请查看日志,因为它们提供了大量详细信息来确定正在发生的事情。它们有时可能难以理解,但如果您向后搜索跟踪,通常会找到您要查找的内容。

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/.npm/_logs/2021-03-05T22_26_21_954Z-debug.log

许多开发人员会复制并粘贴他们在互联网上找到的代码。当您不确定某些东西是如何工作但调试起来很痛苦时需要 sn-ps 是很好的,因为开发人员会使用旧代码并将版本混合在一起。

我对 Python 比较熟悉,但我猜 ESlint 的 Javascript 版本有问题。看看这个Stackoverflow question,因为我认为它回答了你的问题。该解决方案似乎正在使用解析器。

"parser": "babel-eslint"

【讨论】:

    猜你喜欢
    • 2018-07-14
    • 2018-07-12
    • 2023-02-07
    • 2021-09-25
    • 2022-01-09
    • 2021-10-18
    • 1970-01-01
    • 2021-03-23
    • 1970-01-01
    相关资源
    最近更新 更多