7
votes

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
3
Please edit the question to show the complete and minimal code that's failing (not any modified code that works). - Doug Stevenson
@DougStevenson Thanks for your reply Doug. I have updated the question - Mohit Juneja
for the sake of other users, can I suggest making the title of your question more descriptive? Cloud Functions Firebase v1.0 is a bit general. Can't config Cloud Functions Firebase v1.0 or Cloud Functions Firebase v1.0 won't initialize will make it easier for others to scan in case they have the same problem. - JP Lew
I was getting the same errors as you and this answer allowed me to deploy. - S Q

3 Answers

4
votes

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.

1
votes

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.

0
votes

If you've updated the NPM modules, then you are now using the Stable release of Cloud functions v1. There are some minor changes that should be done to your existing code. Please follow the guide below

https://firebase.google.com/docs/functions/beta-v1-diff?utm_source=email&utm_medium=email&utm_campaign=cloud_functions_v1.0

Version 1.0.0 of the Firebase SDK for Cloud Functions introduces some important changes in the API. The primary change, a replacement of event.data format with data and context parameters, affects all asynchronous (non-HTTP) functions. The updated SDK can also be used with firebase-functions-test, a brand new unit testing companion SDK. See Unit Testing Functions for more information.