为啥我的函数没有部署到 Google Cloud Platform?

Posted

技术标签:

【中文标题】为啥我的函数没有部署到 Google Cloud Platform?【英文标题】:Why doesn't my function deploy to Google Cloud Platform?为什么我的函数没有部署到 Google Cloud Platform? 【发布时间】:2020-10-20 17:20:55 【问题描述】:

index.js

const functions = require('firebase-functions');
var admin = require('firebase-admin');
//var app = admin.initializeApp();

export const helloWorld = functions.https.onRequest((request, response) => 
    console.log('HELLO MY SON!')
    response.send("Hello my son");
 );

我只是想将一个简单的 Hello World 功能部署到我的 Google Cloud Platform 项目(当我尝试为我的 Admin SDK 部署功能时也会发生),但是当我在终端中输入 firebase deploy 时,我收到一条错误消息

解析函数触发器时出错

它在我的函数中突出显示 export 作为意外标记。我已经尝试注释掉管理员的初始化(什么也没做),还尝试降级我的 firebase-tools 版本,但这只会导致我的服务器在部署后超时。

谁能强调我的方法哪里出了问题?

更新:我还更改了我的 eslintrc.json 文件的 parserOptions 并添加了一个源类型

eslintrc.json


  "parserOptions": 
    "ecmaVersion": 6,
    "sourceType": "module"
  ,
  "plugins": [
    "promise"
  ],
  "extends": "eslint:recommended",
  "rules": 
    // Removed rule "disallow the use of console" from recommended eslint rules
    "no-console": "off",

    // Removed rule "disallow multiple spaces in regular expressions" from recommended eslint rules
    "no-regex-spaces": "off",

    // Removed rule "disallow the use of debugger" from recommended eslint rules
    "no-debugger": "off",

    // Removed rule "disallow unused variables" from recommended eslint rules
    "no-unused-vars": "off",

    // Removed rule "disallow mixed spaces and tabs for indentation" from recommended eslint rules
    "no-mixed-spaces-and-tabs": "off",

    // Removed rule "disallow the use of undeclared variables unless mentioned in /*global */ comments" from recommended eslint rules
    "no-undef": "off",

    // Warn against template literal placeholder syntax in regular strings
    "no-template-curly-in-string": 1,

    // Warn if return statements do not either always or never specify values
    "consistent-return": 1,

    // Warn if no return statements in callbacks of array methods
    "array-callback-return": 1,

    // Require the use of === and !==
    "eqeqeq": 2,

    // Disallow the use of alert, confirm, and prompt
    "no-alert": 2,

    // Disallow the use of arguments.caller or arguments.callee
    "no-caller": 2,

    // Disallow null comparisons without type-checking operators
    "no-eq-null": 2,

    // Disallow the use of eval()
    "no-eval": 2,

    // Warn against extending native types
    "no-extend-native": 1,

    // Warn against unnecessary calls to .bind()
    "no-extra-bind": 1,

    // Warn against unnecessary labels    
    "no-extra-label": 1,

    // Disallow leading or trailing decimal points in numeric literals
    "no-floating-decimal": 2,

    // Warn against shorthand type conversions
    "no-implicit-coercion": 1,

    // Warn against function declarations and expressions inside loop statements
    "no-loop-func": 1,

    // Disallow new operators with the Function object
    "no-new-func": 2,

    // Warn against new operators with the String, Number, and Boolean objects
    "no-new-wrappers": 1,

    // Disallow throwing literals as exceptions
    "no-throw-literal": 2,

    // Require using Error objects as Promise rejection reasons
    "prefer-promise-reject-errors": 2,

    // Enforce “for” loop update clause moving the counter in the right direction
    "for-direction": 2,

    // Enforce return statements in getters
    "getter-return": 2,

    // Disallow await inside of loops
    "no-await-in-loop": 2,

    // Disallow comparing against -0
    "no-compare-neg-zero": 2,

    // Warn against catch clause parameters from shadowing variables in the outer scope
    "no-catch-shadow": 1,

    // Disallow identifiers from shadowing restricted names
    "no-shadow-restricted-names": 2,

    // Enforce return statements in callbacks of array methods
    "callback-return": 2,

    // Require error handling in callbacks
    "handle-callback-err": 2,

    // Warn against string concatenation with __dirname and __filename
    "no-path-concat": 1,

    // Prefer using arrow functions for callbacks
    "prefer-arrow-callback": 1,

    // Return inside each then() to create readable and reusable Promise chains.
    // Forces developers to return console logs and http calls in promises. 
    "promise/always-return": 2,

    //Enforces the use of catch() on un-returned promises
    "promise/catch-or-return": 2,

    // Warn against nested then() or catch() statements
    "promise/no-nesting": 1
  

 

【问题讨论】:

【参考方案1】:

export 不是 CommonJS 中的有效令牌。因此,您不能在 Node.js 环境中使用它。正确的语法应该是这样的:

exports.helloWorld = functions.https.onRequest((request, response) => 
  console.log('HELLO MY SON!')
  response.send("Hello my son");
);

相关帖子:Getting Unexpected Token Export

【讨论】:

以上是关于为啥我的函数没有部署到 Google Cloud Platform?的主要内容,如果未能解决你的问题,请参考以下文章

ModuleNotFoundError:将 Streamlit 应用程序部署到 Heroku 时没有名为“google.cloud”的模块

Google Cloud Functions - 为啥 GCF 将两个位置参数传递给我的函数?

将参数从 Google Cloud 函数 GET 请求传递到 BigQuery

检索旧版本的 Google Cloud 函数源

如何将本地训练的 TensorFlow 图形文件部署到 Google Cloud Platform?

使用TensorFlow作为依赖项部署Google Cloud Functions