【问题标题】:Firebase functions cannot deploy : SyntaxError: Unexpected token functionFirebase 函数无法部署:SyntaxError: Unexpected token function
【发布时间】:2018-10-22 18:51:50
【问题描述】:

我正在尝试将功能部署到 Firebase,但在部署过程中出现错误

错误:函数未正确部署。

能不能和异步函数联系起来?

实际行为 函数部署时出现错误,cli 显示以下消息:

================控制台日志================

> eslint .
✔  functions: Finished running predeploy script.
i  functions: ensuring necessary APIs are enabled...
✔  functions: all necessary APIs are enabled
i  functions: preparing functions directory for uploading...
i  functions: packaged functions (56.39 KB) for uploading
✔  functions: functions folder uploaded successfully
i  functions: updating function sendContactEmailOAuth...
⚠  functions[sendContactEmailOAuth]: Deployment error.
Function load error: Code in file index.js can't be loaded.
Is there a syntax error in your code?
Detailed stack trace: /user_code/index.js:13
 async function getJwt() {
       ^^^^^^^^

================ 函数 index.js 文件 ================

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


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

   const { JWT } = require('google-auth-library/build/src/index');

   exports.sendContactEmailOAuth = functions.https.onRequest((req, res) => {
 const sender_msg = 'just a test'
 const email = 'contact@lechorodescharentes.org'

 async function getJwt() {
   const client = new JWT(
     functions.config().service_key.client_email,
     null,
     functions.config().service_key.private_key,
     ['https://www.googleapis.com/auth/cloud-platform', 'https://mail.google.com'],
   );
     await client.authorize();
      const url = `https://www.googleapis.com/dns/v1/projects/${functions.config().service_key.project_id}`;
   const res = await client.request({ url });
   console.log(res.data);
 }

getJwt();

  /*  send email with nodemailer to be inserted here */
 });

================ package.json 文件 ================

{
 "name": "functions",
 "description": "Cloud Functions for Firebase",
 "scripts": {
   "lint": "eslint .",
   "serve": "firebase serve --only functions",
   "shell": "firebase functions:shell",
   "start": "npm run shell",
   "deploy": "firebase deploy --only functions",
   "logs": "firebase functions:log"
 },
 "dependencies": {
   "firebase-admin": "~5.12.0",
   "firebase-functions": "^1.0.2",
   "firebase-tools": "^3.18.4",
   "google-auth-library": "^1.4.0",
   "nodemailer": "^4.6.4"
 },
 "devDependencies": {
   "eslint": "^4.12.0",
   "eslint-plugin-promise": "^3.6.0"
 },
 "private": true
 }

【问题讨论】:

  • 能否请您看一下我的答案并考虑将其标记为已接受?

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


【解决方案1】:

如果您在最新版本(例如节点 12)上仍有问题,请在您的 .eslintrc.js 文件中使用 ecmaVersion 解析器选项。

这是一个示例:

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

h/t 发给 Dean 的 original suggestion

【讨论】:

    【解决方案2】:

    截至 2019 年 9 月

    1. 更新 firebase-admin:npm install --save firebase-admin
    2. 更新 firebase 功能:npm install --save firebase-functions
    3. "engines": { "node": "10" } 添加到您的/functions/package.json
    ...
    "dependencies": {
        "firebase-admin": "^8.5.0",
        "firebase-functions": "^3.2.0"
      },
      "devDependencies": {
        "tslint": "~5.19.0",
        "typescript": "~3.6.2"
      },
      "engines": {
        "node": "10"
      }
    ...
    

    截至 2018 年 8 月

    Cloud Functions 现在支持 Node 8 (8.11.1)。查看this blog post

    升级到节点 8

    按照this blog post 中的建议,按照以下步骤升级到节点 8:

    1. 通过npm install --save firebase-functions@latest升级您的firebase-functions版本
    2. 通过npm update -g firebase-tools升级firebase-tools
    3. "engines": { "node": "8" } 添加到您的/functions/package.json

    【讨论】:

    • 这应该是公认的答案。帮了我很多!谢谢玉莲!
    • @erwin 如果您将其标记为已接受的答案会很好,因为从您接受原始答案的那一刻起情况已经发生了变化。大多数用户都希望得到这个答案。
    • 如果有人在 进行更新后遇到 linting 错误,请确保在 .eslintrc.json 中也设置了 "ecmaVersion": 8
    • @DeanStamler 感谢您的提示。我试图将它添加到packages.json,但我无法让它工作。 github.com/eslint/eslint/issues/11976
    • 不幸的是,目前(2020 年 1 月)它对我不起作用,有人知道吗?
    猜你喜欢
    • 2022-06-10
    • 1970-01-01
    • 2019-04-27
    • 2016-10-15
    • 1970-01-01
    • 2020-12-01
    • 2020-08-02
    相关资源
    最近更新 更多