Lance Samaria
Lance Samaria

Reputation: 19622

Firebase Cloud Functions Deploy Error- SyntaxError: Unexpected token '?'

I just updated to:

npm: 8.11.0
node: v16.15.1

New Edit:

I just updated again sudo n latest:

npm: 8.12.1
node: v18.4.0

I'm trying to deploy a new cloud function firebase deploy --only functions:deleteUser but I keep getting a cli error:

Function failed on loading user code. This is likely due to a bug in the user code. Error message: Error: please examine your function logs to see the error cause

When I look at the log:

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

New Edit Again

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
}

Upvotes: 14

Views: 8837

Answers (4)

Lance Samaria
Lance Samaria

Reputation: 19622

Thanks to the tip by @raina77ow in the comments. I had to go inside my package.json file and simply change the node version from 12 to 16

old:

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

new:

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

Update: I posted this a while back, Firebase is now on "node": "20" look here

Upvotes: 26

Chuan
Chuan

Reputation: 3451

Upgrade the Node version from package.json as well as the runtime from firebase.json

Upvotes: 0

Tammo
Tammo

Reputation: 51

The suggested solutions didn't work for me because I already use node version 16. What worked for me was replacing the firebase toolset with the latest version. I was surprised but it cleared the error. You can do that using the following command:

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

Now be aware that --force shouldn't be used unless you want to really replace the existing toolset (In general some should be careful with that command). Sudo was necessary in my case to execute the command with elevated rights. After that firebase deployment should work.

Upvotes: 1

nvvetal
nvvetal

Reputation: 1793

For users in node 12.x version:

npm install --save [email protected] 

Upvotes: 8

Related Questions