2

I am trying to initialize the firebase admin sdk in my functions folder so that my cloud function can access my database with admin read/write access, but when I try to deploy my functions the following error is returned

Error: Error parsing triggers: Cannot find module '/serviceKey.json'

I have tried running npm install in the functions directory but it hasn't helped.

Here is my package.json file

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "serve": "firebase serve --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "dependencies": {
    "cors": "^2.8.4",
    "express": "^4.16.3",
    "firebase": "^5.3.1",
    "firebase-admin": "^5.13.1",
    "firebase-functions": "^2.0.2"
  },
  "private": true
}

Here is my index.js file

{
    const functions = require('firebase-functions');
    const express = require('express');

    const cors = require('cors')({origin: true});

    var firebase = require("firebase");


    var admin = require("firebase-admin");

    require("firebase/auth");
    require("firebase/database");
    //require("firebase/firestore");
    //require("firebase/messaging");
    require("firebase/functions");


    var serviceAccount = require("/serviceKey.json");


    // Initialize the app with a service account, granting admin 
    privileges
    admin.initializeApp({
      credential: admin.credential.cert(serviceAccount),
      databaseURL: "https://<database name>.firebaseio.com"
    });

    const displayqr = express();
    const geturl = express();


    displayqr.get('/displayqr', (request, response) => {
         console.log("response sent");
            response.send("testio/qrdisplay.html");
        });




     exports.displayqr = functions.https.onRequest(displayqr);



     exports.geturl = functions.https.onCall((email) => {// get url 
    function
        });
}
4
  • 2
    /serviceKey.json refers to a file in the root of your file system. That's most likely an error/typo. May be you want ./serviceKey.json? Commented Aug 8, 2018 at 3:56
  • If you want a definitive answer, share your project file structure, especially the location of your service account file relative to the rest of the project. Without that, we can only guess what you intend to happen here. Commented Aug 8, 2018 at 4:03
  • Also as a Functions user, there's no reason to be using a service account JSON file. You should just use application default credentials: admin.initializeApp(); Commented Aug 9, 2018 at 17:07
  • Thanks, why is the dot necessary, that fixed it but I am not sure why Commented Aug 10, 2018 at 16:01

3 Answers 3

5

You put the in the same folder as your index.js and say

var serviceAccount = require("./myfirebaseapp-firebase-adminsdk-my-secret-key.json");

and after that you admin.initializeApp

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I missed the initial period
glad to help. click accept so the question doesn't keep bubbling up if u got your answer.
5

In my case, I had the service-account file at the root of the project. It worked locally, but deploying it was an issue.

../service-account.json  (Root)

What worked for me:

Incase of a Node App, move the file from the root of your project to your functions directory since firebase will host the files at different locations

./service-account.json  (Inside functions directory)

1 Comment

Thank you! Your comment about Firebase storing the root and functions in different locations really helped me out.
3

putting json file inside lib folder worked for me (functions\lib), i think as I am using typescript , firebase will deploy javascript functions from lib file and hence it looks for json file inside lib folder

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.