Firebase Cloud Functions 部署错误 - SyntaxError:意外的标记“?”

问题描述 投票:0回答:4

我刚刚更新到:

npm: 8.11.0
node: v16.15.1

新编辑

我又更新了

sudo n latest
:

npm: 8.12.1
node: v18.4.0

我正在尝试部署新的云功能

firebase deploy --only functions:deleteUser
但我不断收到 cli 错误:

加载用户代码时函数失败。这可能是由于错误 用户代码。错误消息:错误:请检查您的功能日志 查看错误原因

当我查看日志时:

deleteUser

Detailed stack trace: /workspace/node_modules/firebase-admin/lib/app/firebase-namespace.js:84

this.INTERNAL = new FirebaseNamespaceInternals(appStore ?? new lifecycle_1.AppStore());

Provided module can't be loaded. 

Is there a syntax error in your code?

SyntaxError: Unexpected token '?'

at wrapSafe (internal/modules/cjs/loader.js:915:16)
at Module._compile (internal/modules/cjs/loader.js:963:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Module.require (internal/modules/cjs/loader.js:887:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (/workspace/node_modules/firebase-admin/lib/default-namespace.js:19:30)
at Module._compile (internal/modules/cjs/loader.js:999:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)

Could not load the function, shutting down.

Index.js:

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

exports.deleteUser = functions.https.onCall((data, context) => {

    const userID = data.userID;

    admin.auth().deleteUser(userID)
    .then(() => {
        console.log('Successfully deleted userID: ', userID);
        return true // without this Return I get a different error: Each then() should return a value or throw  promise/always-return
    })
    .catch((error) => {
        console.log('Error deleting user: ', error);
    });
});

再次编辑

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": {
    "@google-cloud/logging": "^8.1.1",
    "firebase-admin": "^11.0.0",
    "firebase-functions": "^3.11.0",
    "save": "^2.4.0"
  },
  "devDependencies": {
    "eslint": "^5.12.0",
    "eslint-plugin-promise": "^4.0.1",
    "firebase-functions-test": "^0.2.0"
  },
  "private": true
}
node.js firebase google-cloud-functions
4个回答
25
投票

感谢评论中 @raina77ow 的 tip。我必须进入我的

package.json
文件并将节点版本从 12 更改为 16

旧:

"engines": {
    "node": "12" // causes error
  }

新:

"engines": {
    "node": "16" // error is now gone
  }

更新:我不久前发布了此内容,Firebase 现已上线

"node": "20"


8
投票

对于 Node 12.x 版本的用户:

npm install --save [email protected] 

1
投票

建议的解决方案对我不起作用,因为我已经使用节点版本 16。对我有用的是用最新版本替换 firebase 工具集。我很惊讶,但它清除了错误。您可以使用以下命令来完成此操作:

sudo npm install -g firebase-tools@latest --force

现在请注意,除非您想真正替换现有工具集,否则不应使用 --force (一般来说,有些人应该小心该命令)。在我的情况下,需要 Sudo 才能以更高的权限执行命令。之后 Firebase 部署应该可以工作。


0
投票

package.json
升级 Node 版本以及从
runtime
 升级 
firebase.json

© www.soinside.com 2019 - 2024. All rights reserved.