Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloud Functions Firebase v1.0 won't initialize

With the latest update of firebase cloud functions, I am getting errors while initializing app, as well as database ref.

First Error: Following should work based on Firebase functions v1.0 documentation and samples ( https://github.com/firebase/friendlychat-web/blob/master/cloud-functions/functions/index.js )

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

I get following error on firebase deploy for above code:

Error: Error occurred while parsing your function triggers.

Error: Failed to parse app options file: Error: ENOENT: no such file or directory, open '[object Object]'
    at FirebaseAppError.FirebaseError [as constructor] (/Users/ZZZ/dummy/functions/node_modules/firebase-admin/lib/utils/error.js:39:28)
    at FirebaseAppError.PrefixedFirebaseError [as constructor] (/Users/ZZZ/dummy/functions/node_modules/firebase-admin/lib/utils/error.js:85:28)
    at new FirebaseAppError (/Users/ZZZ/dummy/functions/node_modules/firebase-admin/lib/utils/error.js:119:28)
    at FirebaseNamespaceInternals.loadOptionsFromEnvVar (/Users/ZZZ/dummy/functions/node_modules/firebase-admin/lib/firebase-namespace.js:214:19)
    at FirebaseNamespaceInternals.initializeApp (/Users/ZZZ/dummy/functions/node_modules/firebase-admin/lib/firebase-namespace.js:64:28)
    at FirebaseNamespace.initializeApp (/Users/ZZZ/dummy/functions/node_modules/firebase-admin/lib/firebase-namespace.js:362:30)
    at Object.<anonymous> (/Users/ZZZ/dummy/functions/index.js:5:7)
    at Module._compile (module.js:643:30)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)

This error is resolved if I pass the config file (but it goes against the firebase documentation)

const functions = require('firebase-functions');
const admin = require('firebase-admin');
var config = {
    apiKey: "<APIKEY>",
    authDomain: "<DOMAIN>",
    databaseURL: "<URL>",
    projectId: "<PROJECTID>",
    storageBucket: "<BUCKET>",
    messagingSenderId: "<ID>"
  };
admin.initializeApp(config);//this works

Second Error:When I make database ref call as below , it fails and gives error:

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

var config = {
    apiKey: "<APIKEY>",
    authDomain: "<DOMAIN>",
    databaseURL: "<URL>",
    projectId: "<PROJECTID>",
    storageBucket: "<BUCKET>",
    messagingSenderId: "<ID>"
  };
  const app = admin.initializeApp(config);

  exports.updateUserProfile = functions.database.ref('/UserProfile/{userid}')
    .onCreate((snapshot, context) => {
        console.log(snapshot.val());
    });

Logs:

Running command: npm --prefix "$RESOURCE_DIR" run lint

> functions@ lint /Users/ZZZ/dummy/functions
> 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...

Error: Unexpected token o in JSON at position 1
like image 767
Mohit Juneja Avatar asked Apr 06 '18 07:04

Mohit Juneja


2 Answers

You are experiencing this error because of a bug in firebase CLI version 3.18.1. Try uninstalling the current version and install 3.18.0 and this should solve the error.

npm uninstall -g firebase-tools
npm install -g [email protected]

UPDATE: Firebase CLI version 3.18.2 has been released and the issue is now resolved.

like image 196
Areeb Waseem Avatar answered Nov 15 '22 18:11

Areeb Waseem


I was experiencing this same error. For me the problem appears to have been resolved when I followed these instructions to migrate from a Javascript project to a Typescript project.

It was a bit of a hail mary and might not actually be what fixed it. Still, if you're still stuck it won't hurt to do a quick backup and give it a shot.

like image 25
Andrew Reid Avatar answered Nov 15 '22 18:11

Andrew Reid