Firebase 函数无法部署:SyntaxError: Unexpected token function [英] Firebase functions cannot deploy : SyntaxError: Unexpected token function

查看:30
本文介绍了Firebase 函数无法部署:SyntaxError: Unexpected token function的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to deploy a function to firebase and I get an error during deployment

错误:函数未正确部署.

Error: Functions did not deploy properly.

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

Could it be linked with the async function ?

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

Actual behavior Functions get deployed with errors, the cli shows me the following message:

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

> 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
 }

推荐答案

截至 2019 年 9 月:

  1. 更新 firebase-admin:npm install --save firebase-admin
  2. 更新 firebase-functions:npm install --save firebase-functions
  3. "engines": { "node": "10" } 添加到您的/functions/package.json
  1. Update firebase-admin : npm install --save firebase-admin
  2. Update firebase-functions : npm install --save firebase-functions
  3. Add "engines": { "node": "10" } to your /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).查看这篇博文.

Cloud Functions now support Node 8 (8.11.1). Check out this blog post.

升级到节点 8

按照这篇博文中的建议,按照以下步骤操作升级到节点 8:

As suggested in this blog post, follow these steps to upgrade to Node 8:

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

这篇关于Firebase 函数无法部署:SyntaxError: Unexpected token function的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆